I am trying to host a legacy web application within a Webview2 WPF control and it keeps crashing after it opens a new window and tries to write to the document.
The solution needs to interact with both the initial window and the newly opened window, so I am handling the Webview2 NewWindowRequested event like this:
var deferral = e.GetDeferral();
e.Handled = true;
MainWindow mw = new MainWindow
{
Deferral = deferral,
Args = e
};
mw.Show();
On CoreWebView2InitializationCompleted
if (Deferral != null)
{
Args.NewWindow = wv.CoreWebView2;
Deferral.Complete();
}
Here is the HTML:
Test<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>
I tried handling CoreWebView2 ProcessFailed event, and it fires for both the initial Webview2 and the newly created one as well. ExitCode = -1073741819 ProcessFailedKind = BrowserProcessExited Reason = Unexpected
I am using Webview2 version 100.0.1185.36
Is there a different way I can handle opening the new window to avoid the crash? If I let the new window open using the default simple browser, it works fine. However, I need access to the newly created window.
Any other ideas?
I have created a simple test of this here - https://github.com/attilamn/webview2-document-write-test