簡體   English   中英

自定義 WebView 在模擬器中工作,但不在物理設備中。 Xamarin

[英]Custom WebView working in simulator but not in physical device. Xamarin

我在 Droid 項目中實現了一個自定義 web 視圖渲染器。 問題是,自定義 WebView 在模擬器中工作,但不在物理設備中。

在物理設備 WebView 中僅第一次加載而不是第二次加載。 即使android版本相同,物理設備和模擬器的結果也不同。

您可以在此鏈接中看到完整的代碼Github

  public class MyWebViewRenderer : WebViewRenderer
    {
        public MyWebViewRenderer(Context context) : base(context) { }
        static MyWebView _xwebView = null;
        WebView _webView;
       
        class ExtendedWebViewClient : Android.Webkit.WebViewClient
        {
            public override async void OnPageFinished(WebView view, string url)
            {
                if (_xwebView != null)
                {
                    int i = 10;
                    while (view.ContentHeight == 0 && i-- > 0) 
                        await System.Threading.Tasks.Task.Delay(10);
                    _xwebView.HeightRequest = view.ContentHeight;
                }
                base.OnPageFinished(view, url);
               
            }
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);
            _xwebView = e.NewElement as MyWebView;
            _webView = Control;

            if (e.OldElement == null)
            {
                _webView.SetWebViewClient(new ExtendedWebViewClient());
              
            }
        }

    }

任何幫助表示贊賞。 謝謝!

我又在小米6X Android 9上測試過了。 無論我進入 webview 多少次,它仍然可以正常工作。

所以我建議你第二次進入webview時下載android studio查看日志。

您也可以嘗試在具有不同 android 版本的不同設備上進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM