8

i want to know if there is anyway of stopping the webview2 component from opening a browser window in win forms

What is happening enter image description here

i looked everywhere but could not find one, i did find one though, but it used XAML/UWP

one page used xaml but the code wont work because its XAML and im using c#

NajiMakhoul
  • 1,623
  • 2
  • 16
  • 30
Foreverably
  • 83
  • 1
  • 5
  • hi, not sure, is there a way to catch the new window event – IronMan Feb 02 '21 at 01:08
  • yeah i found it out, but i dont know the code to add it with `public Microsoft.Web.WebView2.Core.CoreWeb View2NewWindowRequestedEventArgs Args;` that's the code i found then another one – Foreverably Feb 02 '21 at 01:12
  • There is [a good practice](https://stackoverflow.com/a/73841289/3193470) to make your app deals with new tabs like **real browsers** – 90Degree Sep 26 '22 at 14:41

4 Answers4

8

To stop the link from opening in a new window, you subscribe to the CoreWebView2_NewWindowRequested as you have found out.

To do that, the easiest way is to subscribe to the CoreWebView2InitializationCompleted first.

In properties window for the WebView2 control, double click CoreWebView2InitializationCompleted - that will auto generate the eventhandler:

Properties windows

Now you add the CoreWebView2_NewWindowRequested eventhandler and set e.NewWindow to the current CoreWebView2.

Here's the code (assuming your WebView2 control is called webView21):

private void WebView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
    webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}

private void CoreWebView2_NewWindowRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
{
    e.NewWindow = webView21.CoreWebView2;
}

Now the link opens in the same window (your WebView2 control).

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • Hi there, tried this but it did not work i think something has to be included in the using or in like the public area, but overall it is a good try. – Foreverably Feb 04 '21 at 22:31
  • What happens? Can you compile? What version of `WebView2` have you installed? – Poul Bak Feb 05 '21 at 13:12
  • Goto 'Project', 'Nuget packages' and update to the latest version, only that contains `CoreWebView2InitializationCompleted`event. – Poul Bak Feb 05 '21 at 16:36
  • it opens a new window when i click a button in the browser i also have the latest update the version edge is on is 90.0.782.0 (Official build) dev (64-bit) – Foreverably Feb 05 '21 at 21:49
  • Have you added the `NewWindowRequested`event handler as showed? Try setting a break point in `CoreWebView2_NewWindowRequested` to see if it's called. I actually tested the code and it should work. – Poul Bak Feb 05 '21 at 22:03
2

To complement @Poul Bak answer I was having this exact problem in VB.Net but all the answers were for C# so im going to post it in here if anyone else needs it.

First make sure to import this:

Imports Microsoft.Web.WebView2.Core
Imports Microsoft.Web.WebView2.WinForms

And then add this 2 events just replace wVBrowser with your Webview2 control name.

Private Sub wVBrowser_CoreWebView2InitializationCompleted(sender As Object, e As CoreWebView2InitializationCompletedEventArgs) Handles wVBrowser.CoreWebView2InitializationCompleted
            AddHandler wVBrowser.CoreWebView2.NewWindowRequested, AddressOf CoreWebView2_NewWindowRequested
        End Sub
Private Sub CoreWebView2_NewWindowRequested(ByVal sender As Object, ByVal e As Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs)
            e.Handled = True
        End Sub
Code_Geass
  • 79
  • 2
  • 10
1

I tried like Poul Bak mentioned above it worked for me, may be your event subscription place might be wrong, try like below.

Subscribe for NewWindowRequested event after CoreWebView2 initialized.

For Ex:

 public Form1()
    {
        InitializeComponent();

        //https://learn.microsoft.com/en-us/microsoft-edge/webview2/get-started/winforms
        InitializeAsync();
     
    }

    async void InitializeAsync()
    {
        await webView.EnsureCoreWebView2Async(null);
        webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
    }

private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
    {
        e.NewWindow = webView.CoreWebView2;
    }

Happy Coding ... !!!!

Maccy
  • 21
  • 2
0

There is a good practice to make your app deals with new tabs like real browsers

90Degree
  • 199
  • 1
  • 5