index:
@Entry
@Component
struct CustomDialogCloseDemo {
@State setTimeOut: number = 0;
build() {
Row() {
Column({ space: 20 }) {
Button('打开自定义弹窗')
.fontSize(20)
.onClick(async () => {
const uiContext = this.getUIContext();
const componentContent = new ComponentContent(this.getUIContext(), wrapBuilder(customDialogBuilder));
uiContext.getPromptAction().openCustomDialog(componentContent, {
onWillDismiss: () => {
uiContext.getPromptAction().closeCustomDialog(componentContent).then(() => {
});
}
});
this.setTimeOut = setTimeout(() => {
uiContext.getPromptAction().closeCustomDialog(componentContent).then(() => {
});
}, 2000);
});
}
.width('100%');
}
.height('100%');
}
} customDialogComponent:
@Builder
export function customDialogBuilder() {
customDialogComponent();
}
@Component
struct customDialogComponent {
aboutToAppear(): void {
console.info('测试弹窗打开');
}
aboutToDisappear(): void {
console.info('测试弹窗关闭');
}
build() {
Column({ space: 30 }) {
Text('弹窗页面')
.fontColor(Color.White)
.height('100%')
.fontSize(30);
}
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center)
.height(150)
.width('90%')
.justifyContent(FlexAlign.SpaceBetween)
.backgroundColor('rgba(148, 148, 148, 1.00)');
}
} import { FrameNode, ComponentContent } from "@kit.ArkUI";
import { BusinessError } from '@kit.BasicServicesKit';
class Params {
text: string = "";
constructor(text: string) {
this.text = text;
}
}
let contentNode: ComponentContent<Params>;
let gUIContext: UIContext;
@Builder
function buildText() {
buildTextComponent()
}
@Component
struct buildTextComponent {
aboutToAppear(): void {
console.info('测试半模态打开')
}
aboutToDisappear(): void {
console.info('测试半模态关闭')
}
build() {
Column() {
Text('')
Button('Update BindSheet')
.fontSize(20)
.onClick(() => {
gUIContext.updateBindSheet(contentNode, {
backgroundColor: Color.Pink,
}, true)
.then(() => {
console.info('updateBindSheet success');
})
.catch((err: BusinessError) => {
console.error(`updateBindSheet error: ${err.code} ${err.message}`);
})
})
Button('Close BindSheet')
.fontSize(20)
.onClick(() => {
gUIContext.closeBindSheet(contentNode)
.then(() => {
console.info('closeBindSheet success');
})
.catch((err: BusinessError) => {
console.error(`closeBindSheet error: ${err.code} ${err.message}`);
})
})
}
}
}
@Entry
@Component
struct UIContextBindSheet {
@State message: string = 'BindSheet';
aboutToAppear() {
gUIContext = this.getUIContext();
contentNode = new ComponentContent(this.getUIContext(), wrapBuilder(buildText));
}
build() {
RelativeContainer() {
Column() {
Button('Open BindSheet')
.fontSize(20)
.onClick(() => {
let uiContext = this.getUIContext();
let uniqueId = this.getUniqueId();
let frameNode: FrameNode | null = uiContext.getFrameNodeByUniqueId(uniqueId);
let targetId = frameNode?.getFirstChild()?.getUniqueId();
uiContext.openBindSheet(contentNode, {
height: SheetSize.MEDIUM,
backgroundColor: Color.Green,
title: { title: "Title", subtitle: "subtitle" }
}, targetId)
.then(() => {
console.info('openBindSheet success');
})
.catch((err: BusinessError) => {
console.error(`openBindSheet error: ${err.code} ${err.message}`);
})
})
}
}
.height('100%')
.width('100%')
}
} ComponentContent创建的openCustomDialog弹窗的关闭过程不涉及页面的销毁,弹窗关闭时不会执行aboutToDisappear()方法。同理,openBindSheet创建的半模态页面关闭过程也不涉及页面的销毁,关闭时同样不会执行aboutToDisappear()方法。
如果想在弹窗关闭后触发弹窗的aboutToDisappear()生命周期函数,可以在promptAction.closeCustomDialog()的异步方法回调里,调用ComponentContent的dispose()方法立即释放当前ComponentContent实例,解除节点之间的绑定关系即可触发aboutToDisappear()生命周期函数。
对index代码做如下修改:
import { ComponentContent } from '@kit.ArkUI';
import { customDialogBuilder } from './customDialogComponent';
@Entry
@Component
struct CustomDialogCloseDemo {
@State setTimeOut: number = 0;
build() {
Row() {
Column({ space: 20 }) {
Button('打开自定义弹窗')
.fontSize(20)
.onClick(async () => {
const uiContext = this.getUIContext();
const componentContent = new ComponentContent(this.getUIContext(), wrapBuilder(customDialogBuilder));
uiContext.getPromptAction().openCustomDialog(componentContent, {
onWillDismiss: () => {
uiContext.getPromptAction().closeCustomDialog(componentContent).then(() => {
clearTimeout(this.setTimeOut);
componentContent.dispose();
});
}
});
this.setTimeOut = setTimeout(() => {
uiContext.getPromptAction().closeCustomDialog(componentContent).then(() => {
componentContent.dispose();
});
}, 2000);
});
}
.width('100%');
}
.height('100%');
}
} customDialogComponent:
@Builder
export function customDialogBuilder() {
customDialogComponent();
}
@Component
struct customDialogComponent {
aboutToAppear(): void {
console.info('测试弹窗打开');
}
aboutToDisappear(): void {
console.info('测试弹窗关闭');
}
build() {
Column({ space: 30 }) {
Text('弹窗页面')
.fontColor(Color.White)
.height('100%')
.fontSize(30);
}
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center)
.height(150)
.width('90%')
.justifyContent(FlexAlign.SpaceBetween)
.backgroundColor('rgba(148, 148, 148, 1.00)');
}
} 实现效果如下:

如果想在半模态页面关闭后触发aboutToDisappear()生命周期函数,可以在closeBindSheet的异步回调里,调用ComponentContent的dispose()方法立即释放当前ComponentContent实例,解除节点之间的绑定关系即可触发aboutToDisappear()生命周期函数。若需再次打开半模态页面,还需重新创建ComponentContent实例。
修改后代码如下:
import { FrameNode, ComponentContent } from "@kit.ArkUI";
import { BusinessError } from '@kit.BasicServicesKit';
class Params {
text: string = "";
constructor(text: string) {
this.text = text;
}
}
let contentNode: ComponentContent<Params>;
let gUIContext: UIContext;
@Builder
function buildText() {
buildTextComponent()
}
@Component
struct buildTextComponent {
aboutToAppear(): void {
console.info('测试半模态打开')
}
aboutToDisappear(): void {
console.info('测试半模态关闭')
}
build() {
Column() {
Text('')
Button('Update BindSheet')
.fontSize(20)
.onClick(() => {
gUIContext.updateBindSheet(contentNode, {
backgroundColor: Color.Pink,
}, true)
.then(() => {
console.info('updateBindSheet success');
})
.catch((err: BusinessError) => {
console.error(`updateBindSheet error: ${err.code} ${err.message}`);
})
})
Button('Close BindSheet')
.fontSize(20)
.onClick(() => {
gUIContext.closeBindSheet(contentNode)
.then(() => {
console.info('closeBindSheet success');
// 关闭后调用dispose释放ComponentContent,触发aboutToDisappear
contentNode.dispose();
// 重新创建ComponentContent以备下次打开
contentNode = new ComponentContent(gUIContext, wrapBuilder(buildText));
})
.catch((err: BusinessError) => {
console.error(`closeBindSheet error: ${err.code} ${err.message}`);
})
})
}
}
}
@Entry
@Component
struct UIContextBindSheet {
@State message: string = 'BindSheet';
aboutToAppear() {
gUIContext = this.getUIContext();
contentNode = new ComponentContent(this.getUIContext(), wrapBuilder(buildText));
}
build() {
RelativeContainer() {
Column() {
Button('Open BindSheet')
.fontSize(20)
.onClick(() => {
let uiContext = this.getUIContext();
let uniqueId = this.getUniqueId();
let frameNode: FrameNode | null = uiContext.getFrameNodeByUniqueId(uniqueId);
let targetId = frameNode?.getFirstChild()?.getUniqueId();
uiContext.openBindSheet(contentNode, {
height: SheetSize.MEDIUM,
backgroundColor: Color.Green,
title: { title: "Title", subtitle: "subtitle" }
}, targetId)
.then(() => {
console.info('openBindSheet success');
})
.catch((err: BusinessError) => {
console.error(`openBindSheet error: ${err.code} ${err.message}`);
})
})
}
}
.height('100%')
.width('100%')
}
}