【问题标题】:Open Link Clicked on Webview2 on edge browser new window在边缘浏览器新窗口上单击 Webview2 打开链接
【发布时间】:2022-06-11 01:56:40
【问题描述】:

我想在 webview2 弹出窗口中单击 HTML 链接打开新的边缘窗口。 下面的代码在同一窗口中打开网址。

private void webView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
 webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}

private void CoreWebView2_NewWindowRequested(object sender,CoreWebView2NewWindowRequestedEventArgs  e)
{
    e.NewWindow = (CoreWebView2)sender;
   
}

【问题讨论】:

    标签: c# webview2


    【解决方案1】:

    如果您想在最终用户的默认 Web 浏览器中打开 URI,而不是在 WebView2 中打开新窗口,您可以取消 CoreWebView2.NewWindowRequested 事件并将 URI 发送到浏览器:

    private void webView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
    {
     webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
    }
    
    private void CoreWebView2_NewWindowRequested(object sender,CoreWebView2NewWindowRequestedEventArgs  e)
    {
        e.Handled = true;
        // No need to wait for the launcher to finish sending the URI to the browser
        // before we allow the WebView2 in our app to continue.
        _ = Windows.System.Launcher.LaunchUriAsync(new Uri(args.Uri));
        // LaunchUriAsync is the WinRT API for launching a URI.
        // Another option not involving WinRT might be System.Diagnostics.Process.Start(args.Uri);
    }
    

    我有一个CoreWebView2.NewWindowRequested handling sample code,它演示了处理 NewWindowRequested 的不同方式。

    【讨论】:

      猜你喜欢
      • 2015-01-11
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      相关资源
      最近更新 更多