调用 window.open 时如何在 WPF/WebView2 应用程序中创建自定义 windows?

How to create custom windows in a WPF / WebView2 application when window.open is called?

我有一个 Webview2 应用程序需要能够拦截 window.open 调用并以某种方式配置生成的 window(例如,使其不可调整大小、无框架、删除默认位置出现的栏等)。

到目前为止,我知道我需要拦截 NewWindowRequested 事件并将 EventArgsNewWindow 属性 分配给新的 Webview2 控件,但是我我无法使其正常工作,而且我还没有找到任何文档列出尝试此操作的示例。

我正在使用 WPF。

另请注意,我是 WPF 和 WebView2 的新手,所以我正在做的事情可能完全没有意义。

这大概是我的代码:

public static async void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e) {
    var obj = e.GetDeferral(); // I think I need to do this to ensure that the resulting window ends up as the return of window.open
    var win = new System.Windows.Window(); // create my WPF window
    var w = new WebView2(); // create my Webview control
    win.Content = w; // site the Webview control?

    await w.EnsureCoreWebView2Async(MainWindow.Webview.CoreWebView2.Environment);

    e.NewWindow = w.CoreWebView2;
    obj.Complete();
}

我看到的问题是 EnsureCoreWebView2Async 返回的 Task 从未完成。

我设置正确吗?为什么 EnsureCoreWebView2Async 没有解决?

看来我必须调用 win.Show() 才能完成 EnsureCoreWebView2Async Task

也许我真的只想在加载 webview 内容后显示 window,但这至少是一个好的开始。