繁体   English   中英

如何在 WebView2 中获得对新 window 的引用?

[英]How do I get reference to the new window in WebView2?

比如说,我正在点击Flashscore web 网站上的一些网球比赛。 弹出一个新的 window。 我想在 WebView2 中捕获第二个 window:

browser.CoreWebView2Ready += delegate
{ 
    browser.CoreWebView2.NewWindowRequested += OnNewWindowRequested;
};

private async void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
    var newWindow = e.NewWindow; //null
}

但是, newWindownull 同时,使用WindowFeatures ,我可以获得新的 window 的高度或宽度:

uint height = e.WindowFeatures.Height;
uint width = e.WindowFeatures.Width;

如何捕获对第二个 window 的引用?

The NewWindowRequested event can let you cancel opening a new window or replace the window with your own, but you cannot have WebView2 open a new window for you and get a reference to that new window.

NewWindowRequested 事件场景:

  1. 在事件处理程序中不采取任何行动或不订阅。 WebView2 将打开一个新的 window,它具有不受最终开发人员控制的最小 UI。
  2. 将事件 args 上的Handled属性设置为true 取消新 window 的打开。 如果window.open是创建新 window 的内容,则返回值为null
  3. 将事件 args 上的Handled属性设置为true ,并使用Uri属性在用户的默认浏览器中打开 URI,或者以其他方式在 WebView2 之外处理新的 window。
  4. 将事件 args 上的Handled属性设置为true ,并将NewWindow属性设置为您创建的新 CoreWebView2。 如果window.open是创建新 window 的内容,则返回值是新的 window ZA8CFDE6331BD59EB66AC96F8911 对应于 CoreWebView2。 您可以通过从 CoreWebView2Environment class 创建一个新的 CoreWebView2 来获取新的 CoreWebView2,或者如果您在 WPF、WinForms 或 WinUI 中使用 WebView2 元素,您可以创建一个新的 WebView2 元素并使用其 CoreWebView2 属性。

您可以在我们的示例应用程序中看到一些使用 NewWindowRequested 事件的示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM