AsynchronousscriptdialogpromptsinWebView2onWindows
创始人
2024-09-21 04:30:32
0

在Windows的WebView2中异步调用脚本对话框时, 你需要使用CustomWebMessageBroker来监听并从脚本中接收消息。

以下是一些代码示例,演示了如何在Windows的WebView2中实现异步调用脚本对话框:

首先,为了接收来自脚本的消息,您需要在核心 WebView2控件中设置一个自定义WebMessageBroker,并在其中处理从脚本发送的消息。下面是创建自定义WebMessageBroker的范例:

// Create a custom WebMessageBroker
myWebView2.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;

private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
   // Handle incoming message from script
   string message = e.TryGetWebMessageAsString();
   if (message == "HelloWorld")
   {
      // Show custom prompt dialog
      // ...
   }
}

接下来,您可以在您的脚本中调用window.chrome.webview.postMessage API并设置消息参数,以向上一步中创建的自定义WebMessageBroker发送消息。以下是一个演示如何在脚本中发送消息给CoreWebView2_WebMessageReceived处理程序的示例:

// Call window.chrome.webview.postMessage to send message to CoreWebView2_WebMessageReceived
window.chrome.webview.postMessage("HelloWorld");

最后,在上述自定义WebMessageBroker的处理程序中,您可以实现在Windows所需的异步prompt方法来显示相应的消息。例如:

private async void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
   string message = e.TryGetWebMessageAsString();
   if (message == "HelloWorld")
   {
      await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
      {
         var promptDialog = new ContentDialog()
         {
            Title = "Prompt",
            Content = "Please enter your name:",
            PrimaryButtonText = "Ok",
            SecondaryButtonText = "Cancel"
         };

         var result = await promptDialog.ShowAsync();
         
         if (result == ContentDialogResult.Primary)
         {
            // Do something with user input
            // ...
         }
      });
   }
}

这里,我们使用了Windows的异步方法Dispatcher.RunAsync,以确保在UI线程上显示prompt对话框。然后,我们创建了一个ContentDialog并等待用户在提示对话框中单击OK或Cancel按钮。最后,我们可以在该对话框中获取用户输入并继续处理它。

相关内容

热门资讯

透视脚本!哈糖大菠萝有挂吗5个... 透视脚本!哈糖大菠萝有挂吗5个常用方法(透视)皇豪辅助(好像有辅助修改器)-哔哩哔哩1、操作简单,无...
这一现象值得深思!wepoke... 这一现象值得深思!wepoker祈福有用吗(透视)蘑菇云辅助使用视频(真是存在有辅助插件)-哔哩哔哩...
透视挂!pokerworld破... 透视挂!pokerworld破解版下载(透视)微信西楚辅助(一贯是有辅助安装)-哔哩哔哩1、这是跨平...
透视能赢!hhpoker德州透... 透视能赢!hhpoker德州透视挂(透视)多乐辅助在哪里可以下载(其实是真的辅助修改器)-哔哩哔哩1...
透视ai代打!智星德州可以透视... 透视ai代打!智星德州可以透视吗(透视)四川游戏家园破解版ios(竟然真的是有辅助安装)-哔哩哔哩1...
现有说明如下!wepoker安... 现有说明如下!wepoker安装教程(透视)雀姬辅助工具(本来真的是有辅助插件)-哔哩哔哩1、雀姬辅...
透视好友!wepoker私人局... 透视好友!wepoker私人局规律(透视)福建天天开心万能辅助(好像真的是有辅助安装)-哔哩哔哩1、...
透视透视!来玩德州破解器(透视... 透视透视!来玩德州破解器(透视)宝宝浙江辅助脚本(一贯是真的辅助安装)-哔哩哔哩小薇(辅助器软件下载...
此事迅速冲上热搜!aapoke... 此事迅速冲上热搜!aapoker怎么提高中牌率(透视)心悦俱乐部游戏辅助(果然真的是有辅助平台)-哔...
突发!aapoker怎么开辅助... 突发!aapoker怎么开辅助器(透视)新上游辅助器(竟然是真的辅助工具)-哔哩哔哩1、完成新上游辅...