onPageShow(){
dialogController.open();
} Tabs(){
TabContent(){
// 页面内容
}
.onWillShow(() => {
dialogController.open();
})
} 以上两个结论均可以通过将弹出弹窗逻辑实现在页面的aboutToAppear()生命周期中得到解决。
示例代码如下:
@CustomDialog
struct CustomDialogExample {
controller?: CustomDialogController;
cancel: () => void = () => {
};
confirm: () => void = () => {
};
build() {
Column() {
Text('这是弹窗')
.fontSize(20)
.height(100)
Button('点我关闭弹窗')
.onClick(() => {
if (this.controller != undefined) {
this.controller.close();
}
})
.margin(20)
}
}
}
@Entry
@Component
struct S2CFK20250709111725583112 {
@State message: string = '弹窗';
dialogController: CustomDialogController | null = new CustomDialogController({
builder: CustomDialogExample({
cancel: () => {
this.onCancel();
},
confirm: () => {
this.onAccept();
}
}),
cancel: this.existApp,
autoCancel: true,
alignment: DialogAlignment.Center,
width: 300,
height: 200,
backgroundColor: Color.White,
shadow: ({
radius: 20,
color: Color.Grey,
offsetX: 50,
offsetY: 0
}),
});
aboutToAppear(): void {
if (this.dialogController != null) {
this.dialogController.open();
}
}
onCancel() {
console.info('Callback when the first button is clicked');
}
onAccept() {
console.info('Callback when the second button is clicked');
}
existApp() {
console.info('Click the callback in the blank area');
}
build() {
Column() {
Text(this.message)
.id('S2CFK20250709111725583112HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.message = 'Welcome';
})
}
.height('100%')
.width('100%')
}
}