目录

对象引用错误导致UI无法刷新

问题现象

在onTouchDown方法中改变状态变量的值后无法引起UI刷新,代码和运行效果如下:

@Component
struct ContainerView {
  @BuilderParam child: () => void
  onTouchDown = () => {
  } 
  onTouchUp = () => {
  }

  build() {
    Column() {
      Button('长按按钮')
        .onTouch((event) => {
          if (event.type === TouchType.Down) {
            this.onTouchDown()
          } else if (event.type === TouchType.Up) {
            this.onTouchUp()
          }
        })
      this.child()
    }
  }
}

@Entry
@Component
struct MainPage {
  @State title: string = '未点击'

  build() {
    Column() {
      ContainerView(
        {
          onTouchDown: this.onTouchDown,
          onTouchUp: () => {
            this.title = 'onTouchUp'
          }
        }
      ) {
        Text(this.title)
          .fontSize(20)
      }
    }
    .width('100%')
    .height('100%')
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center)
  }

  onTouchDown() {
    this.title = 'onTouchDown'
  }
}

背景知识

问题定位

  1. 当直接使用this.onTouchDown时,此时this指针指向的是子组件ContainerView,实际调用的是该组件的onTouchDown空方法。
  2. 使用官网提供的适配方案,即利用lambda函数形式,参考链接如下:bind适配指导案例。

分析结论

this所指向的对象错误,实际需要调用的方法没有被调用。

修改建议

修改onTouchDown函数,具体代码和运行效果如下:

@Component
struct ContainerView {
  @BuilderParam child: () => void;
  // 实际调用的onTouchDown方法
  onTouchDown = () => {
  };
  onTouchUp = () => {
  };

  build() {
    Column() {
      Button('长按按钮')
        .onTouch((event) => {
          if (event.type === TouchType.Down) {
            this.onTouchDown();
          } else if (event.type === TouchType.Up) {
            this.onTouchUp();
          }
        });
      this.child();
    };
  }
}

@Entry
@Component
struct TouchDownCustom {
  @State title: string = '未点击';

  build() {
    Column() {
      ContainerView(
        {
          // 修改后的代码
          onTouchDown: (): void => this.onTouchDown(),
          onTouchUp: () => {
            this.title = 'onTouchUp';
          }
        }
      ) {
        Text(this.title)
          .fontSize(20);
      };
    }
    .width('100%')
    .height('100%')
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center);
  }

  onTouchDown() {
    this.title = 'onTouchDown';
  }
}

常见FAQ

Q:为什么在HarmonyOS开发者文档中没有找到bind关键字的详细说明?

A:ArkTS是一种面向HarmonyOS的开发语言,它基于TypeScript扩展而来,专为HarmonyOS的分布式特性和高性能需求进行了优化,可以在TypeScript相关文档中找到bind方法的详细使用说明。