HarmonyOS 关于Progress组件的value双向绑定问题
在Progress组件示例里,value是用@state声明的
@State value: number = 0
- 1.
如果要在工具类里调用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;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
@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)
}
- 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.
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);
}
}
- 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.
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();
})
})
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
// EntryAbility.ets
onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {
...
AppStorage.setOrCreate('windowStage', windowStage);
}
- 1.
- 2.
- 3.
- 4.
- 5.
HarmonyOS
赞
收藏 0
回答 1
相关问题
HarmonyOS $$双向绑定问题
1060浏览 • 1回复 待解决
数据双向绑定问题——用哪个属性或者空间来完成ArkTS的数据双向绑定
350浏览 • 1回复 已解决
HarmonyOS Progress 修改value时,如何取消动画
561浏览 • 1回复 待解决
父子组件之间如何建立双向的数据绑定?
1492浏览 • 2回复 待解决
HarmonyOS TextInput双向绑定无效
708浏览 • 1回复 待解决
HarmonyOS 无法使用$$双向绑定
1877浏览 • 1回复 待解决
HarmonyOS如何实现双向数据绑定
1461浏览 • 1回复 待解决
@State与@Link的双向绑定,和@Provide与@Comsume的双向绑定的区别是什么?
1336浏览 • 2回复 待解决
#鸿蒙通关秘籍#如何双向绑定TabBar组件和Tab组件的数据?
883浏览 • 1回复 待解决
ArkTS简单类型变量双向数据绑定
2688浏览 • 1回复 待解决
HarmonyOS HSP包中Slider组件使用$$双向绑定数据编译时报错
839浏览 • 1回复 待解决
HarmonyOS 组件外定义的class如何合内部UI做双向绑定,以及interface的用法?
1188浏览 • 1回复 待解决
在同一组件内,组件和变量如何建立双向绑定?
1061浏览 • 2回复 待解决
HarmonyOS 有没有办法让一个@Link双向绑定的变量,同时支持不与父组件绑定,而是手动赋值
877浏览 • 0回复 待解决
HarmonyOS Slider组件onChange返回value失精问题
752浏览 • 1回复 待解决
请教个问题,在不使用双向绑定的情况下如何主动刷新UI?
2454浏览 • 1回复 待解决
使用chart组件progress类型的进度条,如何使进度实时变化,修改segments中的value不行啊?
6702浏览 • 1回复 待解决
字母表与侧标滚动栏的双向绑定
1375浏览 • 1回复 待解决
#鸿蒙通关秘籍#如何在HarmonyOS中使用@Link装饰器实现父子组件的双向数据绑定?
1142浏览 • 1回复 待解决
HarmonyOS 关于Navigation组件的问题
1453浏览 • 1回复 待解决
HarmonyOS 关于video组件的问题
854浏览 • 1回复 待解决
在不使用双向绑定的情况下,是否可以主动通过函数触发组件的刷新
937浏览 • 1回复 待解决
HarmonyOS关于JSVM中JSVM_Value对象生命周期管理的问题
937浏览 • 1回复 待解决
基于Progress组件的进度条
1496浏览 • 1回复 待解决
HarmonyOS Resource获取value问题
965浏览 • 1回复 待解决
类的每次实例化跟上一次都没关系,每一次实例化的value都是0,数据无法与工具类中属性建立双向绑定关于数据双向绑定可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-link-V5