HarmonyOS 关于CustomDialog使用@Link监听数据闪退问题

当把TDialog放到独立函数里面的时候,带上text传参就会闪退,不带上就不会。

代码如下:

import { emitter } from '@kit.BasicServicesKit';
import { TDialog } from './TDialog';

@Entry
@Component
struct Index {
  dialogController?: CustomDialogController | null
  isLandscape: boolean = false
  @State text: string = "当前竖屏"

  build() {
    RelativeContainer() {
      Text(this.text)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .onClick(() => {
      this.openDialog()
    })
    .height('100%')
    .width('100%')
  }

  private openDialog() {
    if (this.dialogController == null) {
      this.dialogController = new CustomDialogController({
        builder: TDialog({ str: this.text }),
        alignment: DialogAlignment.Bottom,
        autoCancel: true,
        customStyle: true,
        cancel: () => {

        }
      });
    }
    this.dialogController?.open();
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 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.
@CustomDialog
export struct TDialog {
  controller?: CustomDialogController
  @Link str: string
  @Provide('coursePthInfo') coursePthInfo: NavPathStack = new NavPathStack()

  build() {
    Column() {
      Text(this.str)
    }
    .width("100%")
    .height(100)
    .borderRadius({
      topLeft: 12,
      topRight: 12
    }).backgroundColor(Color.White)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
HarmonyOS
2024-12-25 13:52:07
1908浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

在ArkUI中CustomDialogController是一个页面级别的组件,其生命周期是与页面绑定的。CustomDialogController在页面中的使用是通过页面的生命周期管理的,例如在页面加载完成后初始化,并在页面退出时销毁。如果将其公开成一个函数,则无法保证其在正确的生命周期阶段被初始化和销毁,而且初始化后也不要再动态对diaogController进行重新赋值,否则会导致不可预期的行为,参考示例如下:

import { AlertDialog } from '@ohos.arkui.advanced.Dialog'

@Entry
@Component
struct Index {
  dialogControllerConfirm: CustomDialogController | undefined
  @State message: string = 'this is a message'

  deleteAccount() {
    this.message = 'nea vale1'
    console.log('deleteAccount')
  }

  aboutToAppear(): void {
    this.dialogControllerConfirm = new CustomDialogController({
      builder: AlertDialog({
        content: '文本文本文本文本文本',
        primaryButton: {
          value: '取消', action: () => {

          },
        },
        secondaryButton: {
          value: '确认',
          fontColor: $r('sys.color.ohos_id_color_warning'),
          action: () => {
            this.deleteAccount()
            console.info('Callback when the second button is clicked')
          }
        },
      }),
      autoCancel: true,
      customStyle: true,
      alignment: DialogAlignment.Bottom
    })
  }

  build() {
    Row() {
      Stack() {
        Column() {
          Text(this.message)
          Button("纯文本弹出框")
            .width(96)
            .height(40)
            .onClick(() => {
              this.dialogControllerConfirm?.open()
            })
        }
        .margin({ bottom: 300 })
      }
      .align(Alignment.Bottom)
      .width('100%')
      .height('100%')
    }
    .backgroundImageSize({ width: '100%', height: '100%' })
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 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.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
分享
微博
QQ
微信
回复
2024-12-25 16:31:27


相关问题
HarmonyOS应用退问题
1287浏览 • 1回复 待解决
HarmonyOS 使用Webview会退
455浏览 • 1回复 待解决
HarmonyOS react-native-webview退问题
647浏览 • 1回复 待解决
HarmonyOS @Link使用问题
364浏览 • 1回复 待解决
HarmonyOS 关于CustomDialog使用
813浏览 • 1回复 待解决
HarmonyOS number toFixed方法退
583浏览 • 1回复 待解决
HarmonyOS 手势处理高概率退
388浏览 • 1回复 待解决
HarmonyOS 退报错 Error code:2100001
506浏览 • 1回复 待解决
HarmonyOS 关于CustomDialog显示层级问题
323浏览 • 1回复 待解决
HarmonyOS 申请短时后台任务退
488浏览 • 1回复 待解决
HarmonyOS PixelMap.rotate接口导致退
495浏览 • 1回复 待解决
HarmonyOS 页面返回时应用退报错
1355浏览 • 1回复 待解决
加载FFMpeg后APP出现退
798浏览 • 1回复 待解决
HarmonyOS升级后,react-native-svg退
514浏览 • 1回复 待解决
打开图库应用时偶尔会退
2962浏览 • 0回复 待解决
关于数据持久化使用问题
877浏览 • 1回复 待解决