
WebView2 NewWindowRequested to react on URL click doesn't work
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
