Android.Webkit.WebView无法在Xamarin.Android中加载url

问题描述 投票:0回答:1

Android.Webkit.WebView无法加载网址

我在主活动类中创建了一个方法,该方法具有WebView但没有加载URL

public void LaunchBrowserView(string authorizationServerUrl)
        {
            try
            {
                var web_view = new Android.Webkit.WebView(this);

                web_view.Settings.JavaScriptEnabled = true;
                web_view.Settings.DomStorageEnabled = true;
                //web_view.Settings.= true;
                //web_view.Settings.AllowContentAccess = true;
                web_view.SetWebViewClient(new MyBrowser());
                web_view.Settings.LoadsImagesAutomatically = true;
                web_view.LoadUrl(authorizationServerUrl);
            }
            catch (System.Exception ex)
            {
            }
        }
class MyBrowser : WebViewClient
    {
        override public bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
        {
            view.LoadUrl(url);
            return false;
        }

        public override void OnReceivedSslError(Android.Webkit.WebView view, SslErrorHandler handler, SslError error)
        {
            base.OnReceivedSslError(view, handler, error);
        }

    }
android xamarin android-intent xamarin.forms xamarin.android
1个回答
0
投票

activity_main.xml

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

MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_main); LaunchBrowserView("https://www.google.com"); }

结果:
enter image description here

我已经在GitHub上上传了,您可以从WebView文件夹下载以供参考。https://github.com/WendyZang/Test.git

已更新:

如果要在没有axml文件的情况下执行此操作,则可以在活动中创建布局。

protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); // Set our view from the "main" layout resource //SetContentView(Resource.Layout.activity_main); RelativeLayout layout_main = new RelativeLayout(this); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent); layout_main.LayoutParameters = layoutParams; WebView webView = new WebView(this); webView.LayoutParameters = layoutParams; layout_main.AddView(webView); SetContentView(layout_main); LaunchBrowserView("https://www.google.com", webView); } public void LaunchBrowserView(string authorizationServerUrl, WebView webView) { try { webView.Settings.JavaScriptEnabled = true; webView.Settings.DomStorageEnabled = true; //web_view.Settings.= true; //web_view.Settings.AllowContentAccess = true; webView.SetWebViewClient(new MyBrowser()); webView.Settings.LoadsImagesAutomatically = true; webView.LoadUrl(authorizationServerUrl); } catch (System.Exception ex) { }

结果:

enter image description here

我已经在GitHub上载,可以从

WebViewDemo2

下载以供参考。https://github.com/WendyZang/Test.git
© www.soinside.com 2019 - 2024. All rights reserved.