ts-component-based-customdialog.md 1.0 KB
Newer Older
Z
zengyawen 已提交
1
# @CustomDialog
Z
zengyawen 已提交
2

3
@CustomDialog装饰器用于装饰自定义弹窗。
Z
zengyawen 已提交
4

Z
zengyawen 已提交
5

H
HelloCrease 已提交
6
```ts
Z
zengyawen 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// custom-dialog-demo.ets
@CustomDialog
struct DialogExample {
    controller: CustomDialogController;
    action: () => void;

    build() {
        Row() {
            Button ("Close CustomDialog")
                .onClick(() => {
                    this.controller.close();
                    this.action();
                })
        }.padding(20)
    }
}

@Entry
@Component
struct CustomDialogUser {
    dialogController : CustomDialogController = new CustomDialogController({
        builder: DialogExample({action: this.onAccept}),
        cancel: this.existApp,
        autoCancel: true
    });

    onAccept() {
        console.log("onAccept");
    }
    existApp() {
        console.log("Cancel dialog!");
    }

    build() {
        Column() {
            Button("Click to open Dialog")
                .onClick(() => {
                    this.dialogController.open()
                })
        }
    }
}
```
T
tianyu 已提交
50 51

![custom-dialog-demo](figures/custom-dialog-demo.gif)