HarmonyOS中如何在类中调用全局的弹窗
前言
相信大家在开发中自定义弹窗都是用的@CustomDialog在每个页面都写一大堆初始化代码,并且在类中使用也是相当难调用,今天给老铁们分享使用 promptAction.openCustomDialog去在类中调用全局的弹窗。
如何使用?
第一步
先到EntryAbility这个文件存取上下文(必要条件)
onWindowStageCreate(windowStage: window.WindowStage): void {
windowStage.loadContent(‘pages/Index’, (err) => {
window.getLastWindow(this.context).then((data: window.Window) => {
let uiContext = data.getUIContext();
AppStorage.setOrCreate(‘UIContext’, uiContext);
});
});
}
1.
2.
3.
4.
5.
6.
7.
8.
写一个Builder(样式自定义)
export class Params {
text: ResourceStr = “”
constructor(text: ResourceStr) {
this.text = text;
}
}
@Builder
export function testTextBuilder(params: Params) {
Column() {
Text(params.text)
.textAlign(TextAlign.Center)
.fontSize(20)
.borderRadius(6)
.backgroundColor(‘rgba(24, 25, 26, 0.85)’)
.fontColor($r(‘sys.color.comp_background_list_card’))
.padding(16)
}
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
万事俱备只欠东风(定义全局类)
hchjzzs.com
m.hchjzzs.com
www.hchjzzs.com
xbtourism.com
m.xbtourism.com
export class DialogModel {
static showDialog(message: string) {
if (message != ‘’) {
let timeId: number = -1
// 获取上下文
let uiContext = AppStorage.get(‘UIContext’) as UIContext
let promptAction = uiContext.getPromptAction();
let contentNode = new ComponentContent(uiContext, wrapBuilder(testTextBuilder), new Params(message));
promptAction.openCustomDialog(contentNode, {
isModal: false,
onWillAppear: () => {
timeId = setTimeout(() => {
promptAction.closeCustomDialog(contentNode)
}, 2000)
},
onDidDisappear: () => {
clearTimeout(timeId)
},
keyboardAvoidMode: KeyboardAvoidMode.NONE,
showInSubWindow: true,
})
}
}
}
齐活开整