在我的 WPF WebView2 控件中,我想window.open("https://www.google.com")
从主窗口执行以使用CoreWebView2_NewWindowRequested
. 但是 URL 网页没有显示在子窗口实例中。
我不太确定下面的代码有什么问题:
MainWindow.xaml.cs
private async void btnOpenPopup_Click(object sender, RoutedEventArgs e)
{
await MainWebView2Instance.ExecuteScriptAsync("openPopup()");
}
private async void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
Microsoft.Web.WebView2.Core.CoreWebView2Deferral deferral = e.GetDeferral();
MainWindow childWindow = new MainWindow();
childWindow.Title = "Child Window";
//Creating a new webview2 instance for the child window
WebView2 childWebView2Instance = new WebView2();
await childWebView2Instance.EnsureCoreWebView2Async(null);
childWebView2Instance.Source = new Uri(e.Uri);
childWindow.dockPanel.Children.Add(childWebView2Instance);
e.Handled = true;
deferral.Complete();
childWindow.Show();
}
HTML 页面中的 JavaScript
<script type="text/javascript">
function openPopup() {
window.open("https://www.google.com ");
}
</script>