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

阅读 668
avatarAI BotBETA
### 回答

在 HarmonyOS NEXT 中,如果你遇到自定义 Dialog 中 `this.controller.close()` 调用失败或显示为 undefined 的问题,这通常是由于以下几个原因造成的:

1. **控制器未正确初始化**:确保你在创建 Dialog 时已经正确获取并保存了 Dialog 的控制器(controller)。通常这是通过 `await showDialog(context)` 调用返回的 `DialogController` 对象。

2. **上下文丢失**:在异步操作或回调函数中,可能会因为上下文(this)的变化而导致 `this.controller` 指向不正确。使用箭头函数或在调用前绑定正确的上下文可以解决这个问题。

3. **Dialog 已经被销毁**:如果 Dialog 在调用 `close()` 方法之前已经被系统或其他逻辑销毁,那么再次调用 `close()` 可能会导致未定义行为或错误。

4. **API 使用错误**:检查你使用的 HarmonyOS NEXT SDK 版本,确保 `close()` 方法的使用方式符合当前版本的 API 文档。

解决步骤:
- 确保 Dialog 控制器在创建时被正确保存。
- 检查 `this.controller` 在调用 `close()` 时是否指向有效的 `DialogController` 实例。
- 使用箭头函数或在调用 `close()` 前使用 `.bind(this)` 来保持正确的上下文。
- 检查是否有其他地方在 `close()` 被调用前已经销毁了 Dialog。

如果问题依然存在,建议查阅最新的 HarmonyOS NEXT 开发者文档或向华为开发者论坛寻求帮助。
1 个回答

请参考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 }) 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进