【问题标题】:How to handle load errors in Xamarin.forms webview如何处理 Xamarin.forms webview 中的加载错误
【发布时间】:2019-10-05 20:59:59
【问题描述】:

我正在为我的客户开发一个应用程序,并在我的 Xamarin.forms 应用程序中配置了一个 WebView。如何处理错误(如地址无法访问、没有互联网等。

我添加了try 子句并捕获异常。但它不起作用,如果没有互联网,默认的无法访问页面出现在 android 和 iOS 上的空白页面

 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”

【问题讨论】:

  • 可以通过'OnNavigating'事件拦截url,然后进行相应处理。
  • 你能用代码回答它吗..

标签: c# xamarin.forms webview


【解决方案1】:

尝试在 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!
    }
  };

【讨论】:

  • 没问题。如果对您有帮助,请将结果标记为有用/正确!
  • 已将其标记为有用并已点赞 .. 只是由于声誉低而不可见
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多