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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

弹窗方法:

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));  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
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%')  
  }  
}
  • 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.

​请参考以上代码:

目前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%')  
  }  
}
  • 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.

​参考:​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 弹框无法非组件里面弹出
640浏览 • 1回复 待解决
HarmonyOS dialog非组件类中无法弹出
876浏览 • 1回复 待解决
HarmonyOS Toast无法弹出
611浏览 • 1回复 待解决
HarmonyOS PromptAction无法弹出Toast
762浏览 • 1回复 待解决
HarmonyOS share弹窗无法弹出
537浏览 • 1回复 待解决
HarmonyOS JsBridge适配问题
724浏览 • 1回复 待解决
HarmonyOS web组件jsBridge通信
794浏览 • 1回复 待解决