Webview2 在新打开的 window 上调用 document.write 时崩溃

Webview2 crashes when calling document.write on newly opened window

我正在尝试在 Webview2 WPF 控件中托管旧版 Web 应用程序,但它在打开新 window 并尝试写入文档后一直崩溃。

该解决方案需要与初始 window 和新打开的 window 交互,所以我处理 Webview2 NewWindowRequested 事件是这样的:

var deferral = e.GetDeferral();

e.Handled = true;
MainWindow mw = new MainWindow
{
    Deferral = deferral,
    Args = e
};

mw.Show();

关于 CoreWebView2InitializationCompleted

if (Deferral != null)
{
    Args.NewWindow = wv.CoreWebView2;
    Deferral.Complete();
}

这里是 HTML:

测试
<button onclick="opentwo()">Open</button>

<script type="text/javascript">
    function opentwo() {
        var two = window.open('');
        two.document.open();
        two.document.write('<html><head><title>test</title></head><body>this is the body</body></html>')
        two.document.close();
    }
</script>

我尝试处理 CoreWebView2 ProcessFailed 事件,它同时为初始 Webview2 和新创建的 Webview2 触发。 退出代码 = -1073741819 ProcessFailedKind = BrowserProcessExited 原因 = 意外

我正在使用 Webview2 版本 100.0.1185.36

是否有其他方法可以处理打开新 window 以避免崩溃的问题?如果我让新 window 使用默认的简单浏览器打开,它工作正常。但是,我需要访问新创建的 window.

还有其他想法吗?

我在这里创建了一个简单的测试 - https://github.com/attilamn/webview2-document-write-test

这是 WebView2 运行时中的错误。它在版本 101.0.1210.8 及更高版本中得到修复。请参阅 Test upcoming APIs and features for information on trying out a preview version of the WebView2 Runtime that should have your bug fixed and the Microsoft Edge Releases calendar 了解修复程序何时会稳定(目前预测为 2022 年 4 月 28 日那一周)。

在撰写本文时,WebView2 Runtime 仍然存在该错误,但 beta、dev 和 canary 通道均已修复。下一个版本的 WebView2 运行时应该包括修复。