Exploring Xamarin.Forms 1.3: Properties Dictionary
Xamarin.Forms 1.3 includes a tidy little way to do simple state persistence between app restarts and when suspending/resuming. Particularly useful if you want to take the user back to their location if the application gets suspended or terminated. This is done through Application.Current.Properties.
It is pretty much just a standard Dictionary of objects but with the added benefit that Xamarin.Forms does the magic of persisting to the file system when your app sleeps, restoring when it resumes and makes it available from *almost* anywhere in your Xamarin.Forms code. It looks something like this:
public <a href="http://iosapi.xamarin.com/monodoc.ashx?link=T%3aSystem.Collections.Generic.IDictionary%602">IDictionary<string, object></a> <b>Properties</b> { get; }
How do I use it?
It couldn’t be easier really. You can access the Properties dictionary in your Xamarin.Forms code by using Application.Current.Properties. For example:
Storing a value
int Id = 42 // some object to store
Application.Current.Properties ["id"] = Id;
Reading a value
if (Application.Current.Properties.ContainsKey("id"))
{
var Id = Convert.ToInt32(Application.Current.Properties ["id"]);
...
}
Note how I checked the existence of the key before accessing it. That’s important (as with any dictionary access).
Clearing a value
if (Application.Current.Properties.ContainsKey("id"))
{
Application.Current.Properties.Remove("id");
}
Nuking everything
Application.Current.Properties.Clear();
So that was all pretty straight forward, right? Well, there are still a couple of gotchas to watch out for.
Gotchas
- The values in the Properties dictionary are only stored when the app goes to sleep. (the OnSleep() method). Which means if your application crashes out with an exception your properties may not be saved. As best I can tell there is no ability to programatically flush the properties, which is a real shame.
- One interesting side effect of the properties only being saved during OnSleep() is that when debugging if you hit the Stop button your values will not be stored. (because OnSleep() is not called). However, you can make the OnSleep() fire by quickly switching away from your app before hitting Stop. (but that kinda sucks).
- Because Properties is just a dictionary of objects you can stick anything you want in there… including your own classes which will get serialized, however, because the entire dictionary is serialized together in a file, *IF* there are issues with deserializing the file then entire Properties collection will be empty. It’s an all or nothing kind of affair.
- I can’t reproduce this right now, but I believe there are problems accessing the Application.Current.Properties property in the constructor of your Application class. And consequently any other methods / objects that are called from your constructor… For example, the constructor of a page you create.
More Information:
Please keep coming back as I look into more interesting Xamarin Forms 1.3 goodness.