HarmonyOS 自定义 Dialog this.controller.close() 关闭失败 或 undefined

HarmonyOS
2024-12-18 14:28:50
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

请参考demo:

@CustomDialog
struct CustomDialogExample {
  controller?: CustomDialogController
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }

  build() {
    Column() {
      Text('可展示在主窗口外的弹窗')
        .fontSize(30)
        .height(100)
      Button('点我关闭弹窗')
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close()
            console.log('关闭成功')
          } else {
            console.log('关闭失败')
          }
        })
        .margin(20)
    }
  }
}

@Entry
@Component
struct CustomDialogUser {
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: () => {
        this.onCancel()
      },
      confirm: () => {
        this.onAccept()
      }
    }),
    cancel: this.existApp,
    autoCancel: true,
    alignment: DialogAlignment.Center,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    showInSubWindow: true,
    isModal: true,
    customStyle: false,
    cornerRadius: 10,
  })

  // 在自定义组件即将析构销毁时将dialogControlle置空
  aboutToDisappear() {
    this.dialogController = null // 将dialogController置空
  }

  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() {
      Button('click me')
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
分享
微博
QQ
微信
回复
2024-12-18 17:30:33
相关问题
HarmonyOS this.controller.close 直接崩溃
164浏览 • 1回复 待解决
HarmonyOS如何自定义组件的Controller
747浏览 • 1回复 待解决
HarmonyOS 自定义全屏dialog
440浏览 • 1回复 待解决
HarmonyOS 自定义Dialog宽度
459浏览 • 1回复 待解决
HarmonyOS 自定义全局dialog
277浏览 • 1回复 待解决
HarmonyOS 用CustomDialog自定义Dialog
713浏览 • 1回复 待解决
HarmonyOS 自定义Dialog高度问题
393浏览 • 1回复 待解决
HarmonyOS 如何封装自定义Dialog
386浏览 • 1回复 待解决
HarmonyOS 自定义dialog相关问题
366浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
856浏览 • 1回复 待解决
HarmonyOS 自定义dialog open无效
601浏览 • 1回复 待解决
HarmonyOS 自定义弹窗关闭问题
414浏览 • 1回复 待解决
HarmonyOS 自定义dialog封装后全局调用
340浏览 • 1回复 待解决