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 jsbridge HarmonyOS版本
28浏览 • 1回复 待解决
HarmonyOS CustomDialogController问题
494浏览 • 1回复 待解决
HarmonyOS jsBridge 最佳实践
255浏览 • 1回复 待解决
HarmonyOS CustomDialogController如何封装
413浏览 • 1回复 待解决
HarmonyOS JsBridge分层设计思想
611浏览 • 1回复 待解决
HarmonyOS JsBridgeHarmonyOS版本sdk
32浏览 • 1回复 待解决
HarmonyOS CustomDialogController组件问题
364浏览 • 1回复 待解决
HarmonyOS CustomDialogController套用Scroll
199浏览 • 1回复 待解决
HarmonyOS CustomDialogController 不显示
255浏览 • 1回复 待解决
HarmonyOS jsbridge能力暴露封装案例
207浏览 • 1回复 待解决
为何picker-view无法显示出来
7249浏览 • 2回复 待解决
jsBridge中如何正确使用this
691浏览 • 1回复 待解决
window模拟器无法弹出软键盘
158浏览 • 1回复 待解决