中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
import { promptAction } from '@kit.ArkUI'; 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); } } let customDialogId: number = 0 @Builder export function customDialogBuilder(content: String) { Column() { Text('弹窗').fontSize(20).height("30%") Text('失败原因:失败原因!').fontSize(16).height("30%") Row() { Button("确认").onClick(() => { promptAction.closeCustomDialog(customDialogId) }) Blank().width(50) Button("取消").onClick(() => { promptAction.closeCustomDialog(customDialogId) }) } .margin({ top: 30 }) }.height(200).padding(5) } export function testPromptDialog() { const that = GlobalContext.getContext().getObject('UIContext') as UIContext; if (that) { promptAction.openCustomDialog({ builder: customDialogBuilder.bind(that, "网络请求失败!") }).then((dialogId: number) => { customDialogId = dialogId; }) } } @Entry @Component struct hwmPage { aboutToAppear(): void { GlobalContext.getContext().setObject('UIContext', this) } build() { Row() { Column() { Button("promptAction弹窗") .onClick(() => { testPromptDialog() }) } .width('100%') } .height('100%') } }