HarmonyOS CustomDialogController无法在jsbridge弹出来

bridge如下:

.javaScriptProxy({  
  object: new GtjaJSBridge(getContext(this), this.wvController),  
  name: "xxxJSBridge",  
  methodList: ["exec"],  
  controller: this.wvController  
})  
  
case "GTJSAPI.certificateInstall":  
new CertificateJSAPI(this.context, this.webviewController, callbackId).certificateInstall(args)  
break

弹窗方法:

let controller = new CustomDialogController({  
  builder: CertificateInstallDialog({  
    cancel: ()=>{  
      controller.close()  
    },  
    confirm: async (passwd)=>{  
      controller.close()  
    }  
  }),  
})  
  
try {  
  controller.open()  
} catch (e) {  
  hilog.debug(0x0000, 'cert', 'jsapi install result: %{public}s', JSON.stringify(e));  
}
HarmonyOS
2024-10-30 10:41:22
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa
<!DOCTYPE html>  
  <html>  
  <head>  
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover">  
  <title></title>  
  <script>  
  function openWindow() {  
    window.myJsb.openDialog()  
  }  
  </script>  
  </head>  
  <body>  
  <button onclick="openWindow()" style="width: 200px;height: 200px">前端调用JSB打开窗口</button>  
  </body>  
  </html>
import webview from '@ohos.web.webview'  
  
export interface JsbObject {  
  openDialog: () => void  
}  
  
@Entry()  
@Component  
struct Page2 {  
  webviewController = new webview.WebviewController()  
  dialogController: CustomDialogController = new CustomDialogController({  
    builder: CustomDialogExample({  
    })  
  })  
  
  jsbObject: JsbObject = {  
    openDialog: () => {  
      this.showDialog(this)  
    }  
  }  
  
  showDialog(context : object) {  
    // 怎么把自定义弹窗调出来  
    this.dialogController.open()  
  }  
  
  build() {  
    Column() {  
      Web({  
        src: "resource://rawfile/aa.html",  
        controller: this.webviewController  
      })  
        .javaScriptAccess(true)  
        .javaScriptProxy({  
          name: "myJsb",  
          object: this.jsbObject,  
          methodList: ["openDialog"],  
          controller: this.webviewController  
  
        })  
        .width('100%')  
    }  
    .backgroundColor(Color.Brown)  
  }  
}  
  
@CustomDialog  
struct CustomDialogExample {  
  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({  
    builder: CustomDialogExample(),  
    alignment: DialogAlignment.Bottom,  
    offset: { dx: 0, dy: -25 } })  
  controller?: CustomDialogController  
  
  build() {  
    Row() {  
      Text("自定义键盘")  
    }  
    .height('40%')  
  }  
}

​请参考以上代码:

目前customDialog不支持在封装的类中弹出,可以考虑使用promptAction.openCustomDialog(支持与页面解耦,支持自定义圆角、字体大小、颜色、背景等)demo如下:​

import { BusinessError } from '@ohos.base';  
import { ComponentContent } from "@ohos.arkui.node";  
  
class Params {  
  text: string = ""  
  constructor(text: string) {  
    this.text = text;  
  }  
}  
  
@Builder  
function buildText(params: Params) {  
  Column() {  
    Text(params.text)  
      .fontSize(50)  
      .fontWeight(FontWeight.Bold)  
      .margin({bottom: 36})  
  }.backgroundColor('#FFF0F0F0').borderRadius(25)  
}  
  
@Entry  
@Component  
struct CustomDialog {  
  @State message: string = "弹窗弹窗"  
  
  build() {  
    Row() {  
      Column() {  
        Button("click me")  
          .onClick(() => {  
            let uiContext = this.getUIContext();  
            let promptAction = uiContext.getPromptAction();  
            let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message));  
            try {  
              promptAction.openCustomDialog(contentNode);  
            } catch (error) {  
              let message = (error as BusinessError).message;  
              let code = (error as BusinessError).code;  
              console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);  
            };  
          })  
      }  
      .width('100%')  
      .height('100%')  
    }  
    .height('100%')  
  }  
}

​参考:​https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-arkui-UIContext.md#opencustomdialog12

分享
微博
QQ
微信
回复
2024-10-30 17:33:33
相关问题
HarmonyOS dialog非组件类中无法弹出
34浏览 • 1回复 待解决
HarmonyOS share弹窗无法弹出
49浏览 • 1回复 待解决
HarmonyOS PromptAction无法弹出Toast
28浏览 • 1回复 待解决
HarmonyOS JsBridge适配问题
36浏览 • 1回复 待解决
HarmonyOS 通知授权弹框无法弹出 -
47浏览 • 1回复 待解决
HarmonyOS jsbridge HarmonyOS版本
116浏览 • 1回复 待解决
HarmonyOS jsBridge 最佳实践
312浏览 • 1回复 待解决