How can I detect when the user clicks on a hyperli

2019-08-04 07:23发布

问题:

I'm trying to detect when the user clicks on a hyperlink in a WebView control from Windows 10. Is there any way to detect this event?

回答1:

If you want to catch the click itself you'll need to do that from inside the web page's JavaScript. Depending on where the page comes from you may be able to inject JavaScript code to do so with WebView.InvokeScriptAsync

If you want to detect the navigation triggered by the click then you can handle the WebView.NavigationStarting or NavigationCompleted events.



回答2:

On Windows 10 I would use the WebView.NewWindowRequested event:

private void WebView1_NewWindowRequested(WebView sender, WebViewNewWindowRequestedEventArgs args)
{
    Debug.WriteLine(args.Uri);
    args.Handled = true; // Prevent the browser from being launched.
}