diff --git a/Xamarin.Forms.Platform.UAP/WebViewRenderer.cs b/Xamarin.Forms.Platform.UAP/WebViewRenderer.cs index 886fda79c..6ba1d0a85 100644 --- a/Xamarin.Forms.Platform.UAP/WebViewRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/WebViewRenderer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel; using Windows.UI.Core; using Windows.UI.Xaml.Controls; @@ -14,6 +14,7 @@ namespace Xamarin.Forms.Platform.UWP { WebNavigationEvent _eventState; bool _updating; + Windows.UI.Xaml.Controls.WebView _internalWebView; const string LocalScheme = "ms-appx-web:///"; // Script to insert a tag into an HTML document @@ -36,10 +37,11 @@ if(bases.length == 0){ string htmlWithBaseTag; // Set up an internal WebView we can use to load and parse the original HTML string - var internalWebView = new Windows.UI.Xaml.Controls.WebView(); + // Make _internalWebView a field instead of local variable to avoid garbage collection + _internalWebView = new Windows.UI.Xaml.Controls.WebView(); // When the 'navigation' to the original HTML string is done, we can modify it to include our tag - internalWebView.NavigationCompleted += async (sender, args) => + _internalWebView.NavigationCompleted += async (sender, args) => { // Generate a version of the script with the correct tag var script = BaseInsertionScript.Replace("baseTag", baseTag); @@ -50,10 +52,12 @@ if(bases.length == 0){ // Set the HTML for the 'real' WebView to the updated HTML Control.NavigateToString(!IsNullOrEmpty(htmlWithBaseTag) ? htmlWithBaseTag : html); + // free up memory after we're done with _internalWebView + _internalWebView = null; }; // Kick off the initial navigation - internalWebView.NavigateToString(html); + _internalWebView.NavigateToString(html); } public void LoadUrl(string url) @@ -235,4 +239,4 @@ if(bases.length == 0){ ((IWebViewController)Element).CanGoForward = Control.CanGoForward; } } -} \ No newline at end of file +}