HarmonyOS openCustomDialog问题

想封装一个公共的弹窗,使用openCustomDialog弹出然后报错。后面看文档发现如果是全局builder需要在组件内部创建一个builder,在内部builder中调用全局builder。这种情况不想封装组件,就想封装成一个类应该怎么写?

export class promptLoading {
  static customDialogId: number = 0

  static open() {
    promptAction.openCustomDialog({
      builder: () => {
        customDialogComponent()
      }
    })
      .then((dialogId: number) => {
        promptLoading.customDialogId = dialogId
      }).catch((err: BusinessError) => {
      console.error('openCustomDialog error: ' + err.code + ' ' + err.message);
    })
  }

  static close() {
    promptAction.closeCustomDialog(promptLoading.customDialogId);
  }
}

@Builder
function customDialogComponent() {
  Column() {
    LoadingProgress()
      .color('red')
      .width(300)
      .height(300)
  }
}

promptLoading.open()
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

customDialogComponent没有bind,参考示例如下:

//Index .est
import { promptAction } from '@kit.ArkUI'
import { customDialogBuilder } from '../component/customDialogBuilder'
import { GlobalContext } from '../component/GlobalContext'

@Entry
@Component
struct Index {
  build() {
    Column() {
      Button("promptAction弹窗")
        .onClick(() => {
          promptAction.openCustomDialog({
            builder: customDialogBuilder.bind(this, '这是弹窗内容')
          }).then((dialogId: number) => {
            GlobalContext.getContext().setObject('customDialogId', dialogId)
          })
        })

    }
    .width(200)
    .height(200)
  }
}
//GlobalContext .est
export class GlobalContext {
  private constructor() {
  }

  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object | undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}
//customDialogBuilder.est
import promptAction from '@ohos.promptAction'
import { GlobalContext } from './GlobalContext'

@Builder
export function customDialogBuilder(dialogTxt: string) {
  TestDialog({ dialogTxt: dialogTxt })
}

@Component
struct TestDialog {
  @State dialogTxt: string = ''

  build() {
    Column() {
      Text(this.dialogTxt).fontSize(40).margin(30)
      Row() {
        Blank().width(50)
        Button("确定").onClick(() => {
          let result = GlobalContext.getContext().getObject('customDialogId') as number
          promptAction.closeCustomDialog(result)
        })
        Button("取消").onClick(() => {
          let result = GlobalContext.getContext().getObject('customDialogId') as number
          promptAction.closeCustomDialog(result)
        })

      }
    }.height(200).padding(5)
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 关于openCustomDialog方法
29浏览 • 1回复 待解决
promptAction.openCustomDialog 全局弹窗
514浏览 • 1回复 待解决
promptAction.openCustomDialog 自定义弹窗
366浏览 • 1回复 待解决