![](/img/trans.png)
[英]How to load HTML page with javascript content in a WebView Xamarin.Forms?
[英]What is the right way to dispose a WebView page in Xamarin.Forms?
我在Xamarin.Forms中有一個可在登錄屏幕后打開WebView的應用程序。
每次用戶注銷並再次登錄時,都會創建一個新的Page和ViewModel。
通過Chrome的“遠程設備”檢查設備,我仍然可以看到較舊的WebView。
對於導航,我使用了幾年前由Evolve App移植的自定義NavigationService。
彈出WebView的Page時,同時處理Page及其ViewModel。
這是彈出WebView頁面並返回到登錄頁面的代碼
公共異步任務NavigateBackAsync(bool IgnoraNavegacao){_isNavigating = true;
try
{
/* Omitted for clarity */
if (currentPage is IDisposable)
((IDisposable)currentPage).Dispose();
if (!(currentPage.GetType() == _homeViewModel))
{
await navigationPage.PopAsync();
}
}
finally
{
_isNavigating = false;
}
頁面的處置 ,其中瀏覽器是XAML中定義的WebView:
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposing)
{
if (disposing)
{
Browser = null;
var bindingContext = BindingContext as IDisposable;
bindingContext?.Dispose();
BindingContext = null;
}
}
這是ViewModel的Dispose :
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
OnNavigatingCommand = null;
OnNavigatedCommand = null;
OnBackButtonPressedCommand = null;
}
}
這是我四次登錄后在Chrome上看到的內容:
有趣的是,它第一次“運行”:在我注銷並再次登錄后,在Chrome上只能看到一個WebView。
請讓我知道是否需要更多信息。
任何指向正確方向的指針將不勝感激。
當此頁面第二時。如果您未使用單例模式創建Web視圖,則不會獲得先前的Web視圖。
因此,有兩種選擇:
一:
使用單例模式時,在此頁面旁邊,您可以獲得舊的Webview。
二:
離開頁面時,可以將WebView的舊實例設置為null
。
在OnDisappearing
方法中:
protected override void OnDisappearing()
{
base.OnDisappearing();
if (null != webView)
{
webView = null;
}
}
或在Navigation.PopAsync()
做之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.