c# - 如何处理 Xamarin.forms webview 中的加载错误

标签 c# xamarin.forms webview

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

我添加了try 子句和catch 异常。但它不起作用,如果没有互联网,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”

最佳答案

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

关于c# - 如何处理 Xamarin.forms webview 中的加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204861/

相关文章:

c# - 在同一页面中显示上传的图像 Asp MVC 4 Razor

c# - 何时取消 Hook Xamarin 自定义呈现器中的事件

android - 为什么我的 YouTube 全屏崩溃了?

javascript - 如何在 QWebView 中启用 localStorage?

c# - 定义具有 1 个以上原型(prototype)的函数

c# - 如何从 float 计算纵横比

c# - 处理 .NET 应用程序中的泄漏(事件类型)

json - 解析 Xamarin 表单中的 XML

Xamarin.Forms 任务与目标不匹配

java - android webview崩溃4.3