HarmonyOS NodeController调用this.rebuild()后,画面没有刷新

使用NodeController,首次进入页面,makeNode被自动回调,绘制出了3个题目框。之后外部调用onSelectBoxChange,更新了nodeArray并手动调用this.rebuild(),makeNode再次被回调,但是页面上并没有出现新绘制的框。

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

参考demo:

import { RenderNode, DrawContext, FrameNode, NodeController } from "@ohos.ArkUI.node"
import drawing from "@ohos.graphics.drawing"

class TextRenderNode extends RenderNode {
  async draw(context: DrawContext) {
    const canvas = context.canvas;
    const pen = new drawing.Pen();
    pen.setStrokeWidth(5);
    pen.setColor({alpha: 255, red: 255, green: 0, blue: 0});
    let path = new drawing.Path();
    path.moveTo(10,10);
    path.arcTo(10, 10, 200, 200, 0, 150);
    path.close();
    canvas.attachPen(pen);
    canvas.drawPath(path);
    canvas.detachPen();
  }
}

class MyNodeController extends NodeController {
  private rootNode: FrameNode | null = null;
  private nodeArray: Array<TextRenderNode> = []
  addMyNode(item:TextRenderNode){
    this.nodeArray.push(item)
  }

  makeNode(uiContext: UIContext): FrameNode | null {
    this.rootNode = new FrameNode(uiContext);
    const renderNode = this.rootNode.getRenderNode();
    if (renderNode === null) {
      return this.rootNode;
    }
    if (renderNode) {
      renderNode.clearChildren()
      this.nodeArray.forEach((item) => {
        renderNode.appendChild(item)
      })
    }
    return this.rootNode;
  }
  // onTouchEvent(event: TouchEvent): void {
  //   this.nodeArray.forEach((node, index) => { // this是undefined,无法获取到当前类的一些参数<---------
  //     console.log(JSON.stringify(this.nodeArray) + '德玛西亚')
  //   })
  // }
}

@Entry
@Component
struct Index {
  @State newNode:TextRenderNode = new TextRenderNode();
  @State nodeCV:MyNodeController = new MyNodeController()
  tempY = 0
  build() {
    Column() {
      Column() {
        NodeContainer(this.nodeCV)
          .width('100%')
          .height(200)
        Button('Invalidate')
          .onClick(() => {
            let newNode = new TextRenderNode()
            newNode.frame = { x: 0, y: this.tempY, width: 200, height: 100 }
            this.nodeCV.addMyNode(newNode)
            this.nodeCV.rebuild()
            this.tempY += 100
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS转屏视频画面90度显示
548浏览 • 1回复 待解决
IF条件变化UI不刷新
760浏览 • 1回复 待解决
DevEco中build和rebuild有什么区别
388浏览 • 1回复 待解决
HarmonyOS拍照调用openSync方法报错
610浏览 • 1回复 待解决
HarmonyOS JSON转换调用对象方法报错
144浏览 • 1回复 待解决
HarmonyOS 摄像头预览画面方向错误
384浏览 • 1回复 待解决
没有调用日历的接口?
6379浏览 • 1回复 待解决