How to create new webView2 window using NewWindowRequested ? #4501
-
Hello, I am intereset scenario number 4 - I want to create my own WPF or WindowForms child window.
` After start application, I go to Console in DevTools (Ctr + Shift + J) and type: So, maybe somebody know how to do it properly ? pls help Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You're mostly on the right track. But there's a couple of issues with your NewWindowRequested handler:
Additional good information about setting the NewWindow property One other thing I'd note is that you usually don't need to use both CoreWebView2InitializeCompleted and EnsureCoreWebView2Async. There's two ways the WebView2 control can initialize its CoreWebView2. The first is if you set the Source property on the WebView2 - this will implicitly begin initialization (and navigation) of the WebView2's CoreWebView2 when the control is initialized. If you don't set the Source property then you need to call EnsureCoreWebView2Async. The CoreWebView2InitializeCompleted event will be raised in either case although its intent is to be used when you are setting the Source property. If you are calling EnsureCoreWebView2Async then when that method asynchronously completes the CoreWebView2 is initialized and you can run code after the call completes. So you don't usually also need the CoreWebView2InitializeCompleted handler. |
Beta Was this translation helpful? Give feedback.
-
Hello all,
What is interesting here (and it took me some time to find that is the reason) - when I try to display new form as dialog and use method regards, |
Beta Was this translation helpful? Give feedback.
-
To create a new WebView2 window using the NewWindowRequested event, you can follow these general steps:
csharp
In this example, when a new window is requested (NewWindowRequested event), a new instance of WebView2 is created and loaded with the requested URI. Then, it's displayed in a new Window instance. Finally, the event is marked as handled (e.Handled = true) to prevent the default behaviour. |
Beta Was this translation helpful? Give feedback.
You're mostly on the right track. But there's a couple of issues with your NewWindowRequested handler: