Google Xamarin.Forms: How to identify website link broken or not reachable in Webview (C# - Xaml) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Friday 13 March 2020

Xamarin.Forms: How to identify website link broken or not reachable in Webview (C# - Xaml)

Introduction:
We know that WebView is a view for displaying web and HTML content in your app. Due to security, sometimes website links will not work with outside network, so we need to identify it to show custom dialog to user. 

Description:
In Xamarin.Forms, WebView raises the following events to help you respond to changes in state:
  • Navigating – event raised when the WebView begins loading a new page.
  • Navigated – event raised when the page is loaded and navigation has stopped.
  • ReloadRequested – event raised when a request is made to reload the current content.
So we can use Navigated event, because this event args has a Result property that helpful to identify url loading status "Failure" or "Success".

In MainPage.xaml
  1. <WebView Navigated="Browser_Navigated" />  

In MainPage.xaml.cs


  • async void Browser_Navigated(object sender, WebNavigatedEventArgs e)  
  •         {  
  •             if (e.Result.ToString().ToLower() == "failure"){  
  •                 await DisplayAlert("""This site can’t be reached""Ok");  
  •             }  
  •         }  

  • FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? I always welcome if you drop comments on this post and it would be impressive.

    Follow me always at @Subramanyam_B

    No comments:

    Post a Comment

    Search Engine Submission - AddMe