【问题标题】:Xamarin Forms: WebView loading time is very highXamarin Forms:WebView 加载时间非常长
【发布时间】:2021-02-04 03:07:42
【问题描述】:

我正在使用WebView 在我的项目中加载网站。它正在加载网站,但速度很慢。加载此site 大约需要 10 到 15 秒。

我尝试了这个thread 的解决方案。我在 application 级别下的清单上添加了android:hardwareAccelerated="true",但没有运气。

对于 iOS 和 Windows,解决方案如下:但我不知道如何创建自定义渲染。

在 iOS 中,尝试使用 WKWebView 而不是 UIWebView。 对于 UWP,请使用 Windows.UI.Xaml.Controls.WebViewcontrol,而不是使用内置的 Xamarin WebView 控件。

我可以获取上面类似 iOS 和 Windows 的示例自定义渲染器吗?

【问题讨论】:

    标签: xamarin.forms webview


    【解决方案1】:

    正在加载网站,但速度很慢

    加载网站的速度取决于很多原因。例如,如果 web 包含许多远程 css/js 文件,它将消耗大量时间。而且还取决于网络性能。

    即使在我这边的浏览器上,您提供的链接也会加载缓慢。如果我们检查源代码,它包含很多远程 css。

    对于 Android ,我们可以创建一个自定义渲染器 webview 来加速它。

    1. 将渲染优先级设置为高。
    2. 启用 dom 存储。
    3. 在加载 html 时,我们可以禁用图像显示,当 加载完成,加载图片 Control.Settings.BlockNetworkImage。
    4. 开启缓存,下次加载可以加载 很快。
    [assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(MyWebviewRender))]
    namespace MyWebviewDemo.Droid
    {
        public class MyWebviewRender : WebViewRenderer
        {
            public MyWebviewRender(Context context) : base(context)
            {
            }
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
            {
                base.OnElementChanged(e);
                Control.Settings.JavaScriptEnabled = true;
                Control.Settings.SetAppCacheEnabled(true);
                Control.Settings.CacheMode = Android.Webkit.CacheModes.Normal;
                Control.Settings.SetRenderPriority(RenderPriority.High);
                Control.Settings.DomStorageEnabled = true;
                Control.Settings.BlockNetworkImage = true;
                Control.SetWebChromeClient(new MyWebChromeClient());
            }
        }
        internal class MyWebChromeClient : WebChromeClient
        {
            public override void OnProgressChanged(Android.Webkit.WebView view, int newProgress)
            {
                base.OnProgressChanged(view, newProgress);
                if (newProgress == 100)
                {
                    // webview load success
    
                    //  //  loadDialog.dismiss();
                    view.Settings.BlockNetworkImage=false;
                }
                else
                {
                    //  webview loading
                    //  loadDialog.show();
                }
            }
        }
    }
    

    在 iOS 中,尝试使用 WKWebView 而不是 UIWebView。对于 UWP,请使用 Windows.UI.Xaml.Controls.WebViewcontrol,而不是使用内置的 Xamarin WebView 控件。

    在你的情况下,即使使用上述解决方案,它也只会有更少的改进。您可以在加载期间添加 ActivityIndi​​cator。如果可以访问该网站,最好将css文件下载到本地并在ContentPage中加载。

    【讨论】:

    • 我们怎样才能indicatorview?
    • 对不起,我的错误,ActivityIndi​​cator。 docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…
    • 初始化 ActivityIndi​​cator 的 IsRunning 值为 true。我们如何将状态更改为假?我们如何理解网站是加载在 UI 上的?
    • 在 webview 的 FinishLoaded 事件中。
    • 我检查了,webview 没有这样的事件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2017-04-17
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多