简单示例如下:
class Model {
title: string
handler: ()=>void
}
@Component
struct Page {
dataSource: DataSource // 外部传入
models: Model[] = this.dataSource.initData()
build {
Column() {
List() {
ForEach(this.models,(model: Model,index) => {
ListItem() { Text(model.title) }
.onClick(() => { model.handler() })
})
}
.width('100%')
.height('100%')
}
}
class DataSource {
dialog?: CustomDialogController
initData(): Model[] {
//....构造一个models数组,这里仅举一例
const model1 = New Model('名称',() => {
/// 在此处构造CustomDialogController,并使用open弹出
this.dialog = new CustomDialogController({
builder: some,
customStyle: true,
alignment: DialogAlignment.Bottom
})
this.dialog.open()
})
}
}
CustomDialogController仅在作为@CustomDialog和@Component struct的成员变量,且在@Component struct内部定义时赋值才有效。请参考: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box-V5\#customdialogcontroller