'Xamarin Forms Android Webview Refresh not working
I'm currently developing a little app which uses the Android WebView to display a specific website. Nothing big or special. But I created a menu where you can set the url to the website. After saving the url and getting back to the webview I can't refresh the webview. Only when I restart the app the new url got loaded. I don't know what I'm doing wrong...
WebView.xaml:
<ContentPage.BindingContext>
<viewModels:WebviewViewModel />
</ContentPage.BindingContext>
<ContentPage.ToolbarItems>
<ToolbarItem Text="reload"
Command="{Binding ReloadWebview}"
CommandParameter="{Binding Source={x:Reference webView}}"
Order="Primary"
Priority="0" />
</ContentPage.ToolbarItems>
<WebView x:Name="webView" Source="{Binding Url}" />
Webview.xaml.cs:
public partial class Webview : ContentPage
{
WebviewViewModel webviewVM;
public Webview()
{
InitializeComponent();
webviewVM = new WebviewViewModel();
}
...
protected async override void OnAppearing()
{
base.OnAppearing();
this.SetUrlAsSource();
webviewVM.ReloadWebview.Execute(webView);
webView.Reload();
...
}
private void SetUrlAsSource()
{
try
{
webView.Source = SecureStorage.GetAsync(Constants.SourceUrl).Result;
}
catch (Exception ex)
{
Debug.WriteLine($"Exception: {ex.Message}, {ex.InnerException}, {ex.Data}");
}
}
}
WebViewViewModel:
class WebviewViewModel : BaseViewModel
{
private string _url;
public WebviewViewModel()
{
Url = SecureStorage.GetAsync(Constants.SourceUrl).Result;
ReloadWebview = new Command<WebView>((key) =>
{
key.Reload();
});
}
public string Url
{
get { return this._url = SecureStorage.GetAsync(Constants.SourceUrl).Result; }
set
{
if (this._url == value)
{
return;
}
this._url = value;
SecureStorage.SetAsync(Constants.SourceUrl, this._url);
OnPropertyChanged();
}
}
public ICommand ReloadWebview { get; }
}
Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
