Blocking WebView Links (Android)

валера карманов 396 Reputation points
2024-10-11T08:25:10.6133333+00:00

Is there any event that will occur when clicking on a link in the "WebView" element?

Developer technologies .NET .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-10-14T01:36:26.6866667+00:00

    Hello,

    Maui Webview provides two navigation events to handle events during and after a page navigating.

    You could refer to the official documentation and sample code below.

    WebView defines a Navigating event that's raised when page navigation starts, and a Navigated event that's raised when page navigation completes. The WebNavigatingEventArgs object that accompanies the Navigating event defines a Cancel property of type bool that can be used to cancel navigation. The WebNavigatedEventArgs object that accompanies the Navigated event defines a Result property of type WebNavigationResult that indicates the navigation result.

    // in xaml
    <WebView  Source="
    https://cn.bing.com/"
    WidthRequest="200" HeightRequest="300" 
                 Navigating="WebView_Navigating" Navigated="WebView_Navigated"/>
    
    // in code-behind
    private void WebView_Navigating(object sender, WebNavigatingEventArgs e)
    {
        Console.WriteLine(e.Url);
    }
     
    private void WebView_Navigated(object sender, WebNavigatedEventArgs e)
    {
        Console.WriteLine(e.Url);
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

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.