HarmonyOS CustomDialogController无法封装成工厂方法

CustomDialogController封装成工厂方法时失效,以下方式dialog正常弹出:

@Entry
@Component
struct Index {
  private shareDialog: CustomDialogController =
    new CustomDialogController({ builder: ShareDialog(), alignment: DialogAlignment.Bottom, customStyle: true })

  //private shareDialog:CustomDialogController = ShareDialogControllerFactory.getShareDialogController()

  build() {
    Row() {
      Button("share")
        .onClick(() => {
          this.shareDialog.open()
        })
    }
  }
}

封装成工厂方法返回后则无法弹出:

@Entry
@Component
struct Index {
  //private shareDialog:CustomDialogController = new CustomDialogController({builder: ShareDialog(), alignment:DialogAlignment.Bottom, customStyle:true})
  private shareDialog: CustomDialogController = ShareDialogControllerFactory.getShareDialogController()

  build() {
    Row() {
      Button("share")
        .onClick(() => {
          this.shareDialog.open()
        })
    }
  }
}

class ShareDialogControllerFactory {
  public static getShareDialogController(): CustomDialogController {
    console.log("get called")
    return new CustomDialogController({ builder: ShareDialog(), alignment: DialogAlignment.Bottom, customStyle: true })
  }
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

自定义弹框不支持在类中定义,可以连同触发弹框的组件一起封装成一个Builder,示例代码如下:

@CustomDialog
struct CustomDialogExample {
  textContent: string = ""
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample(),
  })

  build() {
    Column() {
      Text(this.textContent)
        .fontSize(20)
        .margin({ top: 10, bottom: 10 })
    }
  }
}

@Entry
@Component
struct Index {
  @Builder
  ButtonShowToast(buttonText: string, content: string) {
    Button(buttonText)
      .onClick(() => {
        let dialogController: CustomDialogController = new CustomDialogController({
          builder: CustomDialogExample({ textContent: content }),
        })
        dialogController.open()
      })
  }

  build() {
    Row() {
      Column() {
        this.ButtonShowToast('test1', '111')
        this.ButtonShowToast('test2', '222')
      }
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 如何书写工厂方法
19浏览 • 1回复 待解决
HarmonyOS CustomDialogController封装
35浏览 • 1回复 待解决
HarmonyOS CustomDialogController如何封装
472浏览 • 1回复 待解决
CustomDialogController能否支持再次封装
800浏览 • 1回复 待解决
有没有基于socket封装成熟的网络库?
910浏览 • 1回复 待解决
新人求简单封装方法
4615浏览 • 1回复 待解决
HarmonyOS har包无法封装头文件
466浏览 • 1回复 待解决