如何处理Xamarin.Forms WebView中的负载错误



我正在为我的客户端开发一个应用程序,并已在我的 xamarin.forms 应用程序中配置了WebView。我如何处理错误(喜欢地址无法到达,没有互联网等。(

我添加了尝试子句并捕获异常。但是它不起作用,如果没有互联网,则在iOS上的Android和空白页面上没有默认的页面

 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;}

我希望"无法加载此页面",但是我看到了原始的Android错误页面:" net :: err_address_unreachable"

尝试在WebView导航事件中处理错误。

  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!
    }
  };

最新更新