• Store Lists in Xamarin Forms Settings Plugin

    When storing settings in Xamarin Forms you are probably aware of SettingsPlugin provided by James Montemagno. If not, have a look at it! The plugin only supports simple data types like int, string, bool etc. but not lists. So if you want to store lists with the SettingsPlugin in your Xamarin Forms app you need to serialze them first. Here is a short example how to do it.

    The following code could be used in your settings file. It will store and retrieve a list of Integers. I use standard Newtonsoft Json for (de)serializing so you have to add the Json Package and declare “using Newtonsoft.Json;” in your code. So the input and output is a list of integers, internally a string value is stored in the settings as this is of course supported by the SettingsPlugin.

    If no list is stored yet, I return a empty list instead of null so you could always use e.g. “MyList.Contains(123)” and get a valid result even if your list is not created yet. Of course you could shrink the code if you like but it’s easier to debug in case of problems during setup.

     

     

Leave a comment

If you want to share your opinion, leave a comment.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">