HarmonyOS 使用NodeController嵌套第二层builder无法更新UI

使用NodeController添加builderNode引用了Abuilder,A Builder中又嵌套了子Bbuilder然后通过builderNode.update去刷新UI时只有第一层的A builder会改变,B Builder没改变。

HarmonyOS
2025-01-09 15:39:39
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

示例参考如下:

import { UIContext } from '@ohos.arkui.UIContext';
import { NodeController, BuilderNode, FrameNode } from "@ohos.arkui.node"

class Params {
  text: string = ""

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

// 自定义组件
@Component
struct TextBuilder {
  @Prop message: string = "TextBuilder";

  build() {
    Row() {
      Column() {
        Text(`自定义组件:${this.message}`)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .margin({ bottom: 36 })
          .backgroundColor(Color.Gray)
      }
    }
  }
}

@Builder
function buttonBuilderB(params: Params) {
  Column() {
    Button(`二级builder:${params.text}`)
      .fontSize(30)
      .fontWeight(FontWeight.Bold)
      .margin({ bottom: 36 })
  }
}

@Builder
function buildText(params: Params) {
  Column() {
    Text(`一级builder:${params.text}`)
      .fontSize(30)
      .fontWeight(FontWeight.Bold)
      .margin({ bottom: 36 })
    TextBuilder({ message: params.text }) // 自定义组件

    buttonBuilderB(params)
  }
}

class TextNodeController extends NodeController {
  private rootNode: FrameNode | null = null;
  private textNode: BuilderNode<[Params]> | null = null;
  private message: string = "";

  constructor(message: string) {
    super()
    this.message = message
  }

  makeNode(context: UIContext): FrameNode | null {
    this.textNode = new BuilderNode(context);
    this.textNode.build(wrapBuilder<[Params]>(buildText), new Params(this.message))
    return this.textNode.getFrameNode();
  }

  update(message: string) {
    if (this.textNode !== null) {
      this.textNode.update(new Params(message));
    }
  }
}

@Entry
@Component
struct Index {
  @State message: string = "hello"
  private textNodeController: TextNodeController = new TextNodeController(this.message);
  private count = 0;

  build() {
    Row() {
      Column() {
        Button('Update')
          .onClick(() => {
            this.count += 1;
            const message = "Update " + this.count.toString();
            this.textNodeController.update(message);
          }).margin(100)
        NodeContainer(this.textNodeController)
          .width('100%')
          .height(100)
          .backgroundColor('#FFF0F0F0')
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
分享
微博
QQ
微信
回复
2025-01-09 19:00:40
相关问题
HarmonyOS 需要pullToRefresh二层楼demo示例
814浏览 • 1回复 待解决
callback无法成功更新UI
2593浏览 • 1回复 待解决
嵌套Class的属性变化无法触发UI渲染
1030浏览 • 1回复 待解决
HarmonyOS @Builder UI刷新问题
737浏览 • 1回复 待解决
HarmonyOS @builder方法的ui不刷新
1198浏览 • 1回复 待解决
Navigation级导航嵌套
2428浏览 • 1回复 待解决
HarmonyOS ArkWeb同渲染嵌套能力
1248浏览 • 1回复 待解决
HarmonyOS Native怎么更新UI?
735浏览 • 1回复 待解决