Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store App settings in Xamarin.Forms

How can we store and retrieve App settings as key,value pair in Xamarin.Forms? Like when the app is closed we can store the user preferences and on restarting of the App we are able to get those values.

like image 242
A. Sinha Avatar asked May 17 '16 03:05

A. Sinha


2 Answers

It can be done this way too using Properties Dictionary

for storing data:

Application.Current.Properties ["id"] = someClass.ID;

for fetching data:

if (Application.Current.Properties.ContainsKey("id"))
{
    var id = Application.Current.Properties ["id"] as int;
    // do something with id
}

ref: https://developer.xamarin.com/guides/xamarin-forms/working-with/application-class/#Properties_Dictionary

like image 164
A. Sinha Avatar answered Nov 11 '22 04:11

A. Sinha


The Application object (in VS you get an App class that inherits from it, I think the Xamarin Studio template might be slightly different) has a Properties dictionary that is specifically for this. If you need to make sure your properties get saved right away, there is a Application.SavePropertiesAsync method you can call.

like image 8
Bill Reiss Avatar answered Nov 11 '22 06:11

Bill Reiss