我正在为我的客户开发一个应用程序,并在我的 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/