Xamarin网络视图';网页不可用';错误,如何显示自定义错误消息

Xamarin网络视图';网页不可用';错误,如何显示自定义错误消息,xamarin,webview,error-handling,xamarin.android,Xamarin,Webview,Error Handling,Xamarin.android,这是活动代码 namespace LoadWebPage { [Activity(Label = "LoadWebPage", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.NoTitleBar")] public class Activity1 : Activity { protected override void OnCreate (Bundle bun

这是活动代码

namespace LoadWebPage {
    [Activity(Label = "LoadWebPage", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.NoTitleBar")]
    public class Activity1 : Activity {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our webview and load the local file in for display
            WebView webView = FindViewById<WebView>(Resource.Id.LocalWebView);
            webView.SetWebViewClient (new WebViewClient ());
            webView.LoadUrl("http://www.xamarin.com");

            // Some websites will require Javascript to be enabled
            webView.Settings.JavaScriptEnabled = true;
            //webView.LoadUrl("http://youtube.com");

            // allow zooming/panning            
            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.SetSupportZoom(true);

            // scrollbar stuff            
            webView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay; 
            // so there's no 'white line'            
            webView.ScrollbarFadingEnabled = false;
        }
    }
}
命名空间加载网页{
[活动(Label=“LoadWebPage”,MainLauncher=true,Icon=“@drawable/Icon”,Theme=“@android:style/Theme.NoTitleBar”)]
公共课堂活动1:活动{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
//获取我们的webview并加载本地文件以供显示
WebView WebView=findviewbyd(Resource.Id.LocalWebView);
webView.SetWebViewClient(新的WebViewClient());
webView.LoadUrl(“http://www.xamarin.com");
//有些网站需要启用Javascript
webView.Settings.JavaScriptEnabled=true;
//webView.LoadUrl(“http://youtube.com");
//允许缩放/平移
webView.Settings.BuiltInZoomControls=true;
webView.Settings.SetSupportZoom(true);
//滚动条内容
webView.ScrollBarStyle=ScrollbarStyles.OutsideOverlay;
//所以没有“白线”
webView.ScrollbarFadingEnabled=false;
}
}
}

子类
WebViewClient
并实现两个
OnReceivedError
方法,然后创建
WebViewClient
子类的实例,并通过
WebView.SetWebViewClient
方法分配它

当您收到错误时,发布本机警报对话框、自定义弹出窗口或将WebView重定向到自定义错误页面等

WebViewClient子视图: 用法:
谢谢你的支持,但还是解决不了,这是我的解决方案,你能看一下吗,pls@EdathadanChiefakaArun什么是
net::
错误页面中显示的错误?如果在关闭网络的情况下启动应用程序,我会得到一个空白的网络视图。如果我在网络打开的情况下加载第一个页面,然后关闭网络,链接不工作,不会显示错误消息。@Edatadanchiefakaarun如果您现在正在描述另一个问题,请使用
广播接收器
,并将其注册到
IntentFilter
ConnectionVityManager.ConnectionVityAction
)若要监视网络连接并在连接更改时采取相应措施,…@edatadanchiefakaarun使用广播接收器,并将其注册到IntentFilter(ConnectionManager.ConnectionVityAction)以在断开连接时监视网络连接,禁用WebView并显示一个
警报对话框
,说明您的应用程序需要网络连接才能继续。
public class Client : WebViewClient
{
    public override void OnReceivedError(WebView view, ClientError errorCode, string description, string failingUrl)
    {
        DisplayError(view, description);
    }

    // API 21+
    public override void OnReceivedError(WebView view, IWebResourceRequest request, WebResourceError error)
    {
        DisplayError(view, error.Description);
    }

    void DisplayError(WebView view, string description)
    {
        Toast.MakeText(view.Context, description, ToastLength.Long).Show();
        view.LoadUrl("https://stackoverflow.com");
    }
}
webView.SetWebViewClient(new Client());