使用 CoreWebView2NewWindowRequestedEventArgs 在子 window 中打开 URL
Opening the URL in child window using the CoreWebView2NewWindowRequestedEventArgs
在我的 WPF WebView2 控件中,我想从主 window 执行 window.open("https://www.google.com")
以使用 [=15 打开子 window 中的 URL =].但是 URL 网页没有显示在子 window 实例中。
我不太确定我下面的代码有什么问题:
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();
}
JavaScript 在 HTML 页
<script type="text/javascript">
function openPopup() {
window.open("https://www.google.com ");
}
</script>
注释掉(或删除)以下行:
await childWebView2Instance.EnsureCoreWebView2Async(null);
-- 正在阻塞。您没有使用 CoreWebView2Environment
,因此没有必要使用它。当您为 childWebView2Instance
设置 Source 属性 时,它将隐式初始化 CoreWebView2.
更新:
当点击网页上的 link 时,以下代码将打开子 window,其中网页的 HTML 如下:
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
</head>
<script>
function popuponclick()
{
my_window = window.open("https://www.google.com");
}
</script>
<body>
<div>
<a href="javascript: popuponclick()">Open Popup Window</A>
</div>
</body>
</html>
MainWindow.xaml
...
<wv2:WebView2
Name="webView21"
CoreWebView2InitializationCompleted="webView21_CoreWebView2InitializationCompleted"
Source="http://127.0.0.1:80/index.html"/>
...
webView21_CoreWebView2InitializationCompleted (MainWindow.xaml.cs)
private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(this.Title + " - webView21_CoreWebView2InitializationCompleted");
webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}
将 using 语句添加到 MainWindow.xaml.cs:
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
在MainWindow.xaml.cs
中添加另一个构造函数,可用于为WebView2 控件设置Source 属性。它应该如下所示:
构造函数: (MainWindow.xaml.cs)
...
public MainWindow()
{
InitializeComponent();
}
public MainWindow(string url)
{
InitializeComponent();
webView21.Source = new Uri(url);
}
...
CoreWebView2_NewWindowRequested (MainWindow.xaml.cs)
private void CoreWebView2_NewWindowRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
{
CoreWebView2 cwv2 = (CoreWebView2)sender;
Microsoft.Web.WebView2.Core.CoreWebView2Deferral deferral = e.GetDeferral();
MainWindow childWindow = null;
childWindow = new MainWindow(e.Uri);
childWindow.Title = "Child Window";
childWindow.Show();
e.Handled = true;
deferral.Complete();
}
在我的 WPF WebView2 控件中,我想从主 window 执行 window.open("https://www.google.com")
以使用 [=15 打开子 window 中的 URL =].但是 URL 网页没有显示在子 window 实例中。
我不太确定我下面的代码有什么问题:
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();
}
JavaScript 在 HTML 页
<script type="text/javascript">
function openPopup() {
window.open("https://www.google.com ");
}
</script>
注释掉(或删除)以下行:
await childWebView2Instance.EnsureCoreWebView2Async(null);
-- 正在阻塞。您没有使用 CoreWebView2Environment
,因此没有必要使用它。当您为 childWebView2Instance
设置 Source 属性 时,它将隐式初始化 CoreWebView2.
更新:
当点击网页上的 link 时,以下代码将打开子 window,其中网页的 HTML 如下:
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
</head>
<script>
function popuponclick()
{
my_window = window.open("https://www.google.com");
}
</script>
<body>
<div>
<a href="javascript: popuponclick()">Open Popup Window</A>
</div>
</body>
</html>
MainWindow.xaml
...
<wv2:WebView2
Name="webView21"
CoreWebView2InitializationCompleted="webView21_CoreWebView2InitializationCompleted"
Source="http://127.0.0.1:80/index.html"/>
...
webView21_CoreWebView2InitializationCompleted (MainWindow.xaml.cs)
private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(this.Title + " - webView21_CoreWebView2InitializationCompleted");
webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}
将 using 语句添加到 MainWindow.xaml.cs:
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
在MainWindow.xaml.cs
中添加另一个构造函数,可用于为WebView2 控件设置Source 属性。它应该如下所示:
构造函数: (MainWindow.xaml.cs)
...
public MainWindow()
{
InitializeComponent();
}
public MainWindow(string url)
{
InitializeComponent();
webView21.Source = new Uri(url);
}
...
CoreWebView2_NewWindowRequested (MainWindow.xaml.cs)
private void CoreWebView2_NewWindowRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
{
CoreWebView2 cwv2 = (CoreWebView2)sender;
Microsoft.Web.WebView2.Core.CoreWebView2Deferral deferral = e.GetDeferral();
MainWindow childWindow = null;
childWindow = new MainWindow(e.Uri);
childWindow.Title = "Child Window";
childWindow.Show();
e.Handled = true;
deferral.Complete();
}