WebView2 NewWindowRequested to react on URL click doesn't work

D vK 1 Reputation point
2021-04-14T14:35:27.757+00:00

I am working on a WebView2 project, a new HTML browser based on Edge Chromium and a real step forward compared to the old IE based component.

However, when I click a URL in this control, it always opens a new Webview2 Runtime component (without menu's) showing the URL. I want a click executed in an existing window and e.g. a Ctrl or Shift click should open in my default browser (Firefox).

Several webpages refer to NewWindowRequested. This event does not seem to be fired when I click an URL in a page. Why not? I use this code:

public MainWindow()
// From: https://learn.microsoft.com/en-us/microsoft-edge/webview2/gettingstarted/wpf
{
InitializeComponent();
InitializeAsync();
}

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

private void CoreWebView2_NewWindowRequested(object sender,CoreWebView2NewWindowRequestedEventArgs e)
{
// BELOW SOME CODE SAMPLES I FOUND WHICH COULD CHANGE A URL CLICK BEHAVIOR BUT THIS METHOD IS NEVER CALLED
e.NewWindow = (CoreWebView2)sender;
e.Handled = true;
}

Microsoft Edge Microsoft Edge development
Developer technologies C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.