gpt4 book ai didi

Xamarin Webview ' Webpage not available ' 错误,如何显示自定义错误消息

转载 作者:行者123 更新时间:2023-12-03 07:49:28 24 4
gpt4 key购买 nike

这是事件代码。

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

最佳答案

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

当您收到错误时,发布 native AlertDialog、自定义弹出窗口或将 WebView 重定向到自定义错误页面等...

WebViewClient subview :

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());

关于Xamarin Webview ' Webpage not available ' 错误,如何显示自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45388721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com