HarmonyOS 使用promptAction.updateCustomDialog怎么更新弹窗的内容?

根据文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-uicontext-V5#updatecustomdialog12

是能更新弹窗的位置,但是怎么更新弹窗的内容呢?代码如下:

import { BusinessError } from '@kit.BasicServicesKit';  
import { ComponentContent } from '@kit.ArkUI';  
@Entry  
@Component  
struct CustomDialogPage {  
  @State message: string = 'Hello World';  
  build() {  
    Row() {  
      Column() {  
        Button("click me")  
          .onClick(() => {  
            this.message = "Hello"  
            let uiContext = this.getUIContext();  
            let promptAction = uiContext.getPromptAction();  
            let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message));  
            try {  
              promptAction.openCustomDialog(contentNode,{alignment:DialogAlignment.Top});  
            } 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}`);  
            };  
            setTimeout(() => {  
              try {  
                this.message = "World"//我尝试更新弹窗的文案  
                promptAction.updateCustomDialog(contentNode, { alignment: DialogAlignment.Bottom });//并改变弹窗的位置  
              } catch (error) {  
                let message = (error as BusinessError).message;  
                let code = (error as BusinessError).code;  
                console.error(`updateCustomDialog args error code is ${code}, message is ${message}`);  
              };  
            }, 2000);   //2秒后自动更新了弹窗位置,但没有更新弹窗文案  
  
            setTimeout(()=>{  
                promptAction.closeCustomDialog(contentNode)  
            },4000)//4秒后关闭弹窗  
          })  
      }  
      .width('100%')  
      .height('100%')  
    }  
    .height('100%')  
  }  
}  
class Params {  
  text: string = ""  
  constructor(text: string) {  
    this.text = text;  
  }  
}  
@Builder  
function buildText(params: Params) {  
  Column() {  
    Text(params.text)  
      .fontSize(50)  
      .fontWeight(FontWeight.Bold)  
      .margin({bottom: 36})  
  }.backgroundColor('#FFF0F0F0')  
}
HarmonyOS
2024-10-10 10:31:14
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

代码如下:

import { UIContext } from '@ohos.arkui.UIContext';  
import { ComponentContent } from "@ohos.arkui.node";  
import { BusinessError } from '@ohos.base';  
export class myTestClass {  
  ctx: UIContext;  
  myNode: ComponentContent<Object>;  
  params: Object  
  options: Object  
  constructor(context: UIContext, obj?: Object) {  
    this.ctx = context;  
    this.params = obj  
  }  
  setCtx(context) {  
    this.ctx = context;  
  }  
  getCtx() {  
    return this.ctx;  
  }  
  setBuilderNode(node: ComponentContent<Object>) {  
    this.myNode = node;  
  }  
  setOptions(opts: Object) {  
    this.options = opts  
  }  
  openTestDialog() {  
    console.error("lyly111----" + this.ctx);  
    if (this.myNode != null) {  
      this.ctx.getPromptAction().showToast({ message: 'sdahcdshafcbhjsah' })  
      this.ctx.getPromptAction()  
        .openCustomDialog(this.myNode, this.options)  
        .then(() => {  
          console.info('lyeee------openCustomDialog complete!!!');  
        })  
        .catch((error: BusinessError) => {  
          let message = (error as BusinessError).message  
          let code = (error as BusinessError).code  
          console.error(`lyeee------openCustomDialog args error code is ${code}, message is ${message}`);  
        })  
    }  
  }  
  updateTestDialog(params: Object) {  
    this.myNode.update(params)  
  }  
  updateDialogOptions(node: ComponentContent<Object>, opts: Object) {  
    if (node != null) {  
      this.ctx.getPromptAction()  
        .updateCustomDialog(node, opts)  
        .then(() => {  
          console.info('lyeee------updateCustomDialog complete!!!');  
        })  
        .catch((error: BusinessError) => {  
          let message = (error as BusinessError).message  
          let code = (error as BusinessError).code  
          console.error(`lyeee------updateCustomDialog args error code is ${code}, message is ${message}`);  
        })  
    }  
  }  
  closeTestDialog(node: ComponentContent<Object>) {  
    if (node != null) {  
      this.ctx.getPromptAction()  
        .closeCustomDialog(node)  
        .then(() => {  
          console.info('lyeee------closeCustomDialog complete!!!');  
        })  
        .catch((error: BusinessError) => {  
          let message = (error as BusinessError).message  
          let code = (error as BusinessError).code  
          console.error(`lyeee------closeCustomDialog args error code is ${code}, message is ${message}`);  
        })  
    }  
  }  
}
// index.ets  
import { UIContext } from '@ohos.arkui.UIContext';  
import { getMyUiContext } from '../entryability/EntryAbility';  
import { myTestClass } from './test';  
import { ComponentContent } from "@ohos.arkui.node"  
import promptAction from '@ohos.promptAction'  
import { BusinessError } from '@ohos.base';  
let customDialogId: number = 0  
@Builder  
function customDialogBuilder() {  
  Column() {  
    Text('Custom dialog Message').fontSize(10)  
    Row() {  
      Button("确认").onClick(() => {  
        promptAction.closeCustomDialog(customDialogId)  
      })  
      Blank().width(50)  
      Button("取消").onClick(() => {  
        promptAction.closeCustomDialog(customDialogId)  
      })  
    }  
    TextInput()  
  }.height(200).padding(5).backgroundColor(Color.Yellow)  
}  
@Builder  
function customDialogBuilderE() {  
}  
class Params {  
  text: string = ""  
  okCallback: () => void  
  
  constructor(text: string, okCallback: () => void) {  
    this.text = text;  
    this.okCallback = okCallback  
  }  
}  
@Builder  
function buildText(params: Params) {  
  Column() {  
    Button('sdcbjadsbc').margin(10)  
    Text(params.text)  
      .fontSize(50)  
      .fontWeight(FontWeight.Bold)  
      .width(300)  
      .height(200)  
      .borderRadius(30)  
      .onClick(() => {  
        params.okCallback()  
        console.info('lyeee------buildText click!!!!');  
      })  
  }.backgroundColor(Color.Yellow)  
  .onClick(() => {  
    promptAction.showToast({  
      message: "sjkfnjhv bhjfd"  
    })  
  })  
  .rotate({  
    x: 0,  
    y: 0,  
    z: 1,  
    centerX: '50%',  
    centerY: '50%',  
    angle: 300  
  })  
}  
function clickAction(testCallback: () => void, message: string, newTestCallback: () => void) {  
  let ctx: UIContext | null = getMyUiContext()  
  let contentNode = new ComponentContent(ctx as UIContext, wrapBuilder(buildText), new Params(message, testCallback))  
  let ttt: myTestClass = new myTestClass(ctx as UIContext, new Params(message, testCallback))  
  if (ctx != null) {  
    ttt.setBuilderNode(contentNode);  
    ttt.setOptions({  
      alignment: DialogAlignment.TopStart,  
      autoCancel: false,  
      offset: { dx: 0, dy: 50 },  
      onWillDismiss: (dismissDialog: DismissDialogAction) => {  
        console.info("lyeee-----reason=" + JSON.stringify(dismissDialog.reason))  
        console.info("lyeee-----dialog onWillDismiss")  
        if (dismissDialog.reason == DismissReason.PRESS_BACK) {  
          dismissDialog.dismiss()  
        }  
        if (dismissDialog.reason == DismissReason.TOUCH_OUTSIDE) {  
          dismissDialog.dismiss()  
        }  
      },  
      onDidAppear: () => {  
        console.info("lyeee----customDialog:onDidAppear()")  
      },  
      onDidDisappear: () => {  
        console.info("lyeee----customDialog:onDidDisappear()")  
      },  
      onWillAppear: () => {  
        console.info("lyeee----customDialog:onWillAppear()")  
      },  
      onWillDisappear: () => {  
        console.info("lyeee----customDialog:onWillDisappear()")  
      },  
      transition: TransitionEffect.asymmetric(TransitionEffect.OPACITY  
        .animation({ duration: 3000, curve: Curve.Sharp })  
        .combine(TransitionEffect.scale({ x: 1.5, y: 1.5 }).animation({ duration: 3000, curve: Curve.Sharp })),  
        TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth })  
          .combine(TransitionEffect.scale({ x: 0.5, y: 0.5 }).animation({ duration: 100, curve: Curve.Smooth })))  
    });  
    ttt.openTestDialog();  
    ComponentContent(ctx as UIContext, wrapBuilder(buildText), new Params('dhjsch', testCallback))  
    )  
    setTimeout(() => {  
      if (contentNode != null) {  
        ttt.updateTestDialog(new Params('NEW NEW NEW NEW', newTestCallback));  
      }  
  }  
}  
  
@Entry  
@Component  
struct Index {  
  @State message: string = "hello"  
  testCallback() {  
    console.info('lyeee-------testCallback!!!')  
  }  
  newTestCallback() {  
    console.info("lyeee-----dialog newTestCallback!!!!!!");  
  }  
  build() {  
    Row() {  
      Column() {  
        Column() {  
          Text(this.message)  
            .fontSize(50)  
            .fontWeight(FontWeight.Bold)  
            .onClick(() => {  
              clickAction(this.testCallback, this.message, this.newTestCallback)  
            })  
        }  
        .width('100%')  
        Column() {  
          Text(this.message)  
            .fontSize(50)  
            .fontWeight(FontWeight.Bold)  
            .onClick(() => {  
              promptAction.openCustomDialog({  
                builder: customDialogBuilder.bind(this),  
                showInSubWindow: true,  
                DismissDialogAction  
              }).then((dialogId: number) => {  
                customDialogId = dialogId  
              }).catch((error: BusinessError) => {  
                let message = (error as BusinessError).message  
                let code = (error as BusinessError).code  
                console.error(`lyeee------promptAction.openCustomDialog args error code is ${code}, message is ${message}`);  
              })  
            })  
        }  
        .width('100%')  
        Button('AlertDialog Set Duration')  
          .onClick(() => {  
            AlertDialog.show(  
              {  
                title: 'AlertDialog 1',  
                message: 'Set Animation Duration open 3 second, close 100ms',  
                autoCancel: true,  
                alignment: DialogAlignment.Top,  
                offset: { dx: 0, dy: -20 },  
                gridCount: 3,  
                transition: TransitionEffect.asymmetric(TransitionEffect.OPACITY  
                  .animation({ duration: 3000, curve: Curve.Sharp })  
                  .combine(TransitionEffect.scale({ x: 1.5, y: 1.5 })  
                    .animation({ duration: 3000, curve: Curve.Sharp })),  
                  TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth })  
                    .combine(TransitionEffect.scale({ x: 0.5, y: 0.5 })  
                      .animation({ duration: 100, curve: Curve.Smooth }))),  
                confirm: {  
                  value: 'button',  
                  action: () => {  
                    console.info('Button-clicking callback')  
                  }  
                },  
                cancel: () => {  
                  console.info('Closed callbacks')  
                }  
              }  
            )  
          })  
        TextInput().width(400).margin({ top: 300 })  
      }  
      .width('100%')  
      .height('100%')  
    }  
    .height('100%')  
  }  
}
分享
微博
QQ
微信
回复
2024-10-10 18:09:16
相关问题
promptAction.openCustomDialog 全局弹窗
479浏览 • 1回复 待解决
promptAction.openCustomDialog 自定义弹窗
348浏览 • 1回复 待解决
Canvas绘制内容如何动态更新
1656浏览 • 1回复 待解决
HarmonyOS Native怎么更新UI?
242浏览 • 1回复 待解决