简体   繁体   中英

How to handle load errors in Xamarin.forms webview

I'm developing an app for my client and have configured a WebView in my Xamarin.forms application. How do I handle errors ( like Address Unreachable, no internet, etc.. )

I have added try clause and catch exception . But it doesn't work and the default unreachable page comes on android and a blank page on iOS if there is no Internet

 try{WebView.Source = "http://viva-t.000webhostapp.com/vivaapp";}
catch (Exception){var htmlSource = new HtmlWebViewSource();
                htmlSource.Html = @"<html><body><h1>An Error!!!!</h1><p>Could not be loaded</p></body></html>";
                WebView.Source = htmlSource;}

I expect "This page could not be loaded" but instead I see the original android error page: "net::ERR_ADDRESS_UNREACHABLE"

Try handling the error as below in the webview navigated event.

  var htmlSource = new HtmlWebViewSource();
  htmlSource.Html = @"<html><body><h1>An Error!!!!</h1><p>Could not be loaded</p></body></html>";
  var wb = new WebView();
  wb.Source = htmlSource;
  wb.Navigated += (s, e) =>
  {
    if(e.Result!= WebNavigationResult.Success)
    {
         //Handle error here!
    }
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM