HarmonyOS 关于Progress组件的value双向绑定问题

在Progress组件示例里,value是用@state声明的

@State value: number = 0

如果要在工具类里调用Progress组件,怎么创建双向绑定的value?

class ProgressDialogParams {
  title: string = "";
  text: string = "";
  value: number = 0;
  cancel: Function;
  constructor(title: string, text: string, value: number, cancel: Function) {
    this.title = title;
    this.text = text;
    this.value = value;
    this.cancel = cancel;
  }
}
@Builder
function buildProgress(params: ProgressDialogParams) {
  Column() {
    Column() {
      Text(params.title)
        .width('100%')
        .padding(10)
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
      Text(params.text)
        .width('100%')
        .fontSize(16)
        .padding(10)
      Row() {
        Progress({value: params.value, total: 100, type:ProgressType.Linear})
          .style({strokeWidth: 20, enableSmoothEffect: true})
      }
      .margin({ bottom: 10 })
      Row() {
        Button("取消", {
          type: ButtonType.Normal,
          buttonStyle: ButtonStyleMode.NORMAL
        }).onClick(() => {
          params.cancel();
        })
      }
    }
    .width('90%')
    .borderRadius(borderRadiuses(20))
    .backgroundColor('#FFF0F0F0')
    .padding({
      top: 20,
      bottom: 20,
      left: 10,
      right: 10
    })
  }
  .height('100%')
  .justifyContent(FlexAlign.Center)
}
export class ProgressDialogFactory {
  windowStage: window.WindowStage = AppStorage.get('windowStage') as window.WindowStage;
  uiContext = this.windowStage.getMainWindowSync().getUIContext();
  pa = this.uiContext.getPromptAction();
  contentNode: ComponentContent<Params> | null = null;
  openDialog(title: string, text: string, value: number) {
    let cancel = () => {
      this.closeDialog();
    }
    try {
      this.contentNode =
        new ComponentContent(this.uiContext, wrapBuilder(buildProgress), new ProgressDialogParams(title, text, value, cancel));
      this.pa.openCustomDialog(this.contentNode, {
        autoCancel: false
      });
    } catch (error) {
      let message = (error as BusinessError).message;
      let code = (error as BusinessError).code;
      console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
    }
  }
  closeDialog() {
    this.pa.closeCustomDialog(this.contentNode);
  }
}
export const downloadFile = (url: string, path: string = 'DOWNLOAD') => {
  let context = getContext() as common.UIAbilityContext;
  let filePath = `${context.filesDir}/${path}`;
  let value = 0; // 此处声明value
  const controller: ProgressDialogFactory = new ProgressDialogFactory();
  controller.openDialog('版本更新', '下载中,请稍候...', value);
  request.downloadFile(context, {
    url,
    filePath
  }, (err: BusinessError, downloadTask: request.DownloadTask) => {
    if (err) {
      console.error(JSON.stringify(err))
      return;
    }
    downloadTask.on('progress', (size: number, total: number) => {
      value = Math.ceil(size / total) * 100; // 此处value改变不会影响Progress进度
    })
    downloadTask.on('complete', async () => {
      console.log(JSON.stringify(result));
      controller.closeDialog();
    })
  })
}
// EntryAbility.ets
onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {
  ...
  AppStorage.setOrCreate('windowStage', windowStage);
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

类的每次实例化跟上一次都没关系,每一次实例化的value都是0,数据无法与工具类中属性建立双向绑定关于数据双向绑定可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-link-V5

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS $$双向绑定问题
318浏览 • 1回复 待解决
HarmonyOS 无法使用$$双向绑定
900浏览 • 1回复 待解决
HarmonyOS如何实现双向数据绑定
507浏览 • 1回复 待解决
ArkTS简单类型变量双向数据绑定
1516浏览 • 1回复 待解决
字母表与侧标滚动栏双向绑定
816浏览 • 1回复 待解决
HarmonyOS 关于video组件问题
44浏览 • 1回复 待解决
HarmonyOS 关于Navigation组件问题
495浏览 • 1回复 待解决
基于Progress组件进度条
572浏览 • 1回复 待解决
HarmonyOS Resource获取value问题
60浏览 • 1回复 待解决
HarmonyOS 获取Resource中value问题
481浏览 • 1回复 待解决