首页 文章

Android上的Xamarin WebView显示多个实例

提问于
浏览
3

我使用Xamarin.Forms创建了一个混合应用程序 . 它使用多个WebView(XLabs HybridWebViews) . 我在iOS上使用相同的应用程序,它工作正常,而在Android上,它逐渐减慢 . 当我执行chrome://在设备上检查它显示同一页面的多个WebView时,看起来更像隐藏的WebView仍然在内存中并且正在减慢应用程序的速度 .

如何在Android上销毁隐藏的WebView实例?

以下是应用程序上chrome:// inspect的截图:

enter image description here

1 回答

  • 2

    一般来说你应该:

    • 将WebViews实例设置为null .

    • (optionaly)使用 GC.Collect(); 强制进行垃圾回收

    如果还不够,请创建WebView的自定义渲染器并添加:

    protected override void Dispose(bool disposing)
    {
        if (disposing && Element != null)
        {
            if (Control != null)
            {
                    Control.StopLoading();
                    Control.ClearHistory();
                    Control.ClearCache(false);
                    Control.LoadUrl("about:blank");
                    Control.FreeMemory();
                    Control.PauseTimers();
            }
        }
    
        base.Dispose(disposing);
    }
    

    它应该解决你的问题 .

相关问题