question

DvK-1758 avatar image
0 Votes"
DvK-1758 asked TimonYang-MSFT commented

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://docs.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;
}







dotnet-csharpms-edge
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Check if the handler is called when you right-click a link and select “Open link in new window”.

It seems to work. How to reproduce the problem?

0 Votes 0 ·

Hello,

Thanks for your quick reply!

Right click Open link in new window indeed calls CoreWebView2_NewWindowRequested. My question was: why is it not called when I left click the same URL? And when I just continued to work on it, it worked there too. No idea what I changed....I'll post the whole code when it is fully doing what I want.

Dick

0 Votes 0 ·

0 Answers