Xamarin Forms:WebView 加载时间非常长
Xamarin Forms: WebView loading time is very high
我正在使用 WebView
在我的项目中加载网站。它正在加载网站,但速度很慢。加载这个 site.
大约需要 10 到 15 秒
我尝试了这个 thread 的解决方案。我在 application
级别下的清单中添加了 android:hardwareAccelerated="true"
,但没有成功。
对于iOS和Windows,解决方案如下:但我不知道如何创建自定义渲染。
In iOS, try to use WKWebView instead of UIWebView.
For the UWP, use Windows.UI.Xaml.Controls.WebViewcontrol instead of using the built-in Xamarin WebView control.
我可以获取 iOS 和 Windows 的示例自定义渲染器吗?
It is loading websites but very slow
加载网站的速度会高达很多原因。例如,如果 web 包含许多远程 css/js 文件,则会消耗大量时间。而且还要看网络性能。
即使在我这边的浏览器上,您提供的 link 加载速度也很慢。如果我们检查源代码,它包含很多远程css。
对于 Android ,我们可以创建一个自定义渲染器 webview 来加速它。
- 将渲染优先级设置为高。
- 启用 dom 存储。
- 当加载 html 时,我们可以禁用图像显示,当
加载完成,加载图片
Control.Settings.BlockNetworkImage.
- 启用Cache,下次加载时可以加载
很快。
[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();
}
}
}
}
In iOS, try to use WKWebView instead of UIWebView. For the UWP, use Windows.UI.Xaml.Controls.WebViewcontrol instead of using the built-in Xamarin WebView control.
在你的情况下,即使使用上述解决方案,它也只会有较少的改进。您可以在加载过程中添加一个 ActivityIndicator。如果可以访问该网站,最好将css文件下载到本地并在ContentPage中加载它们。
我正在使用 WebView
在我的项目中加载网站。它正在加载网站,但速度很慢。加载这个 site.
我尝试了这个 thread 的解决方案。我在 application
级别下的清单中添加了 android:hardwareAccelerated="true"
,但没有成功。
对于iOS和Windows,解决方案如下:但我不知道如何创建自定义渲染。
In iOS, try to use WKWebView instead of UIWebView. For the UWP, use Windows.UI.Xaml.Controls.WebViewcontrol instead of using the built-in Xamarin WebView control.
我可以获取 iOS 和 Windows 的示例自定义渲染器吗?
It is loading websites but very slow
加载网站的速度会高达很多原因。例如,如果 web 包含许多远程 css/js 文件,则会消耗大量时间。而且还要看网络性能。
即使在我这边的浏览器上,您提供的 link 加载速度也很慢。如果我们检查源代码,它包含很多远程css。
对于 Android ,我们可以创建一个自定义渲染器 webview 来加速它。
- 将渲染优先级设置为高。
- 启用 dom 存储。
- 当加载 html 时,我们可以禁用图像显示,当 加载完成,加载图片 Control.Settings.BlockNetworkImage.
- 启用Cache,下次加载时可以加载 很快。
[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();
}
}
}
}
In iOS, try to use WKWebView instead of UIWebView. For the UWP, use Windows.UI.Xaml.Controls.WebViewcontrol instead of using the built-in Xamarin WebView control.
在你的情况下,即使使用上述解决方案,它也只会有较少的改进。您可以在加载过程中添加一个 ActivityIndicator。如果可以访问该网站,最好将css文件下载到本地并在ContentPage中加载它们。