At https://developer.xamarin.com/guides/xamarin-forms/working-with/app-lifecycle/#Persistence we're told that calling await Application.Current.SavePropertiesAsync()
immediately after adding a key to the Properties
dictionary will save the Properties
across restarts. Unfortunately, I'm not observing this behavior in my application. My code is as follows:
private async void OnServerHostNameEntryCompleted(object sender, string hostname, string successmsg, string failmsg)
{
try
{
Application.Current.Properties["server"] = hostname;
await Application.Current.SavePropertiesAsync ();
Device.BeginInvokeOnMainThread (() => DisplayAlert ("Success", successmsg, "OK") );
}
catch (Exception ex)
{
Device.BeginInvokeOnMainThread (() =>
DisplayAlert ("Failed", failmsg + " Info: " + Environment.NewLine + ex, "OK") );
}
}
Does anyone see anything wrong with this? It never seems to save. The code that uses the server
entry looks like this:
var svr = new EntryCell ();
svr.Label = "Server";
if (Application.Current.Properties.ContainsKey ("server"))
svr.Text = Application.Current.Properties["server"] as string;
else
svr.Text = Client.DefaultServer;
The cell always displays the value of
DefaultServer
after restarting the application, no matter how long I wait after setting the settings before closing my app.