今天学习了一下使用Xamarin中的webview控件来打开网页
首先,先创建了一个layout,在里面放置一个webview控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView1" />
</LinearLayout>
然后新建一个Activity
public class ShopActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Shop);
// Create your application here
WebView _webview = FindViewById<WebView>(Resource.Id.webView1);
_webview.LoadUrl("https://shop444140054.taobao.com/?spm=a230r.7195193.1997079397.2.JxOGq1");
_webview.Settings.JavaScriptEnabled = true;
_webview.Settings.SetSupportZoom(true);
_webview.Settings.BuiltInZoomControls = true;
_webview.Settings.UseWideViewPort = true;
_webview.Settings.DisplayZoomControls = false;
_webview.SetWebViewClient(new ExtWebViewClient());
}
}
public class ExtWebViewClient : WebViewClient
{
public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
{
handler.Proceed();
//base.OnReceivedSslError(view, handler, error);
}
}
起初我在 ExtWebViewClient中重写了ShouldOverrideUrlLoading
public override bool ShouldOverrideUrlLoading(WebView view,string url)
{
view.LoadUrl(url);
return true;
}
但是此方法在遇到非http、https开头的url时,会出现位于xxxx://XXXXX.XXXXXXX.XXXX的网页无法加载,因为net::ERR_UNKNOWN_URL_SCHEME