Xamarin.Android 嵌入web端界面

  在程序中嵌入Web端界面。

首先在前台界面上创建一个webview

<android.webkit.WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView1" />

然后后台代码提供调用的界面

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using Android.Net.Http;

namespace App5
{
    [Activity(Label = "App5", MainLauncher = true)]
    public class MainActivity : Activity
    {
        
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

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

        /// <summary>
        /// 跳转回来刷新数据
        /// </summary>
        protected override void OnResume()
        {
            base.OnResume();

            GetListData();
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        void GetListData()
        {
            WebView _webview = FindViewById<WebView>(Resource.Id.webView1);
            _webview.LoadUrl("https://www.baidu.com/");
            _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);
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/swjian/p/9968742.html