目录

openBindSheet引用颜色资源不随系统深浅色模式切换变化

问题现象

使用openBindSheet打开半模态弹窗,弹窗内容引用了资源颜色,并且深浅模式下配置颜色不同,切换深浅色模式后,弹窗中内容颜色不会发生变化,需要关闭后重新打开才能生效。问题复现代码如下:

import { FrameNode, ComponentContent } from "@kit.ArkUI";
import { BusinessError } from '@kit.BasicServicesKit';


class Params {
  text: string = "";
  constructor(text: string) {
    this.text = text;
  }
}


let contentNode: ComponentContent<Params>;


@Builder
function buildText(params: Params) {
  Column() {
    Text(params.text)
      .fontColor($r("app.color.fontcolor"))
}


@Entry
@Component
struct UIContextBindSheet {
  @State message: string = 'BindSheet';
  uiContext = this.getUIContext()


  build() {
    RelativeContainer() {
      Column() {
        Button('Open BindSheet')
          .fontSize(20)
          .onClick(() => {
            contentNode = new ComponentContent(this.uiContext, wrapBuilder(buildText), new Params(this.message));
            let uniqueId = this.getUniqueId();
            let frameNode: FrameNode | null = this.uiContext.getFrameNodeByUniqueId(uniqueId);
            let targetId = frameNode?.getFirstChild()?.getUniqueId();
            this.uiContext.openBindSheet(contentNode, {
              height: SheetSize.MEDIUM,
              backgroundColor: $r("app.color.start_window_background"),
              title: { title: "Title", subtitle: "subtitle" }
            }, targetId)
              .then(() => {
                console.info('openBindSheet success');
              })
              .catch((err: BusinessError) => {
                console.error('openBindSheet error');
              })
          })
      }
    }
    .height('100%')
    .width('100%')
  }
}

背景知识

解决方案

openBindSheet中的内容须传入自定义节点ComponentContent,该节点内容刷新须自行管理,以避免频繁刷新带来的性能开销。从而导致引用颜色资源不随系统深浅色模式变化的现象。解决方案如下: