HarmonyOS 自定义弹窗不能显示问题

@Entry
@Component
struct JKPromptPage {
  build() {
    Button("Custom Dialog Controller").onClick(() => {
      //可以弹窗代码
      // const controller =
      //   new CustomDialogController({ customStyle: true, builder: CustomDialogExample({}), })
      // controller.open()
      //不能弹窗的代码
      new JKPrompt().show()
    })
  }
}

@CustomDialog
struct CustomDialogExample {
  controller: CustomDialogController
  build() {
    Column() {
      Text('我是内容')
        .fontSize(20)
        .margin({ top: 10, bottom: 10 })
    }
    .backgroundColor("#FF0000")
    .width("100%")
  }
}

class JKPrompt {
  show() {
    const controller =
      new CustomDialogController({ customStyle: true, builder: CustomDialogExample({}), })
    controller.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.

如上代码,将弹窗抽到JKPrompt().show方法就不能显示弹窗了。

使用场景:我们业务代码需要经过一定判断才决定是否弹窗,需要将这个逻辑在多个页面公用,所以需要抽一个show方法

HarmonyOS
2024-12-24 16:25:45
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

请参考示例:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column({ space: 20 }) {
        Button('Toast显示')
          .onClick(() => {
            new GlobalDialog().showDialog($r('app.media.startIcon'), 'test')

            setTimeout(()=>{
              new GlobalDialog().hide()
            },2000)
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

@CustomDialog
struct _GlobalDialog {
  controller: CustomDialogController
  close: () => void = () => {
  }
  image: ResourceStr | undefined = undefined
  @State msg: string = ""

  build() {
    Column() {
      Text(this.msg)
        .fontColor(Color.White)
        .width(px2vp(200))
        .height(px2vp(200))

      Image(this.image)
        .fitOriginalSize(true)
        .objectFit(ImageFit.None)
    }
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .padding(12)
    .margin(30)
    .backgroundColor(Color.Black)
    .borderRadius(10)
    .shadow({
      radius: 10,
      color: Color.Gray,
      offsetX: 3,
      offsetY: 0
    })
  }
}

let _dialogController: CustomDialogController | null
let _cancelCallBack: (() => void) | undefined
@Component
export struct GlobalDialog {
  showDialog(
    image: Resource | undefined,
    msg: string,
    cancelCallBack?: () => void,
    alignment: DialogAlignment = DialogAlignment.Center,
    offset: Offset = { dx: 0, dy: 0 },
    showInSubWindow: boolean = false,
    isModal: boolean = false,
  ): void {
    this.hide()
    _cancelCallBack = cancelCallBack
    let animate: AnimateParam = {
      duration: 90,
      delay: 0,
      curve: Curve.EaseInOut
    }

    _dialogController = new CustomDialogController({
      builder: _GlobalDialog({ image: image, msg: msg }),
      autoCancel: false,
      cancel: () => {
        _dialogController = null
        if (_cancelCallBack) {
          _cancelCallBack()
        }
      },
      customStyle: true,
      alignment: alignment,
      offset: offset,
      maskColor: 0x01000000,
      openAnimation: animate,
      closeAnimation: animate,
      showInSubWindow: showInSubWindow,
      isModal: isModal,
    })
    _dialogController.open()
  }

  hide() {
    if (_dialogController) {
      _dialogController.close()
      _dialogController = null
      if (_cancelCallBack) {
        _cancelCallBack()
      }
    }
  }

  build() {
  }
}
  • 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.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
分享
微博
QQ
微信
回复
2024-12-24 17:19:36
相关问题
HarmonyOS 自定义弹窗问题
1662浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
1389浏览 • 1回复 待解决
HarmonyOS 自定义弹窗控制问题
1018浏览 • 1回复 待解决
HarmonyOS 自定义弹窗关闭问题
967浏览 • 1回复 待解决
HarmonyOS 自定义弹窗层级问题
1038浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog 问题
838浏览 • 1回复 待解决
HarmonyOS 自定义弹窗 (CustomDialog)问题
1330浏览 • 1回复 待解决
HarmonyOS 自定义弹窗封装问题
929浏览 • 1回复 待解决
HarmonyOS 自定义弹窗封装后不显示
1247浏览 • 1回复 待解决
自定义弹窗使用相关问题
1783浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog调用问题
1173浏览 • 1回复 待解决
HarmonyOS 自定义弹窗刷新问题
721浏览 • 1回复 待解决
HarmonyOS 自定义弹窗点击跳转问题
825浏览 • 1回复 待解决
HarmonyOS 自定义弹窗部分问题答疑
1268浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
1324浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1997浏览 • 1回复 待解决
HarmonyOS 自定义弹窗初始化问题
793浏览 • 1回复 待解决