2

I am currently trying to save a list in Xamarin forms. I use this code snippet:

var list = new List<myClass> ();

Application.Current.Properties["myList"] = list;
await Application.Current.SavePropertiesAsync();

When I then close the app and use this code...

if (Application.Current.Properties.ContainsKey("myList"))
{

}

...I cannot reach this if statement.

I read something about people having issues saving a list with this solution but they solved it by converting it to a string. I am a bit unsure on how to do this. If I do this...

Application.Current.Properties["myList"] = list.ToString();

...the app crashes.

I saw that there is a plugin called "Settings" that I might need to use instead in case there isn't a solution to this problem but I would prefer to work with my current code if possible.

Carlos Rodrigez
  • 1,347
  • 1
  • 15
  • 32

1 Answers1

8

The Properties dictionary can only serialize primitive types for storage. Attempting to store other types (such as List can fail silently).

It means that you can't save List because it's not a primitive type of object.
You can serialize it to JSON string for example then it will work.
Code example:

var jsonValueToSave = JsonConvert.SerializeObject(myList);
Application.Current.Properties["myList"] = jsonValueToSave;
await Application.Current.SavePropertiesAsync();

Don't forget to deserialize JSON to List<string> when loading the value back.

Note that yourList.ToString() will not work in this case. More info here.

P.S.: Get familiar with the official documentation & check this post on another related thread.

Community
  • 1
  • 1
EvZ
  • 11,889
  • 4
  • 38
  • 76
  • I tried with this code: `var jsonRequest = JsonConvert.SerializeObject(list); HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json"); Application.Current.Properties["myList"] = content; await Application.Current.SavePropertiesAsync();` but it does not change the outcome – Carlos Rodrigez Apr 23 '17 at 16:48
  • HttpContent is not a primitive type, that's why it's not working. Serializing to JSON string is enough. I updated my answer with code example. – EvZ Apr 23 '17 at 16:52
  • HttpContent is not a "primitive type". You have to serialize the List to a String – Alessandro Caliaro Apr 23 '17 at 16:52
  • Alright! Seems to work now :) Awesome. I am a bit unsure on how to get the value out now though, with this code: `foreach (var data in Application.Current.Properties["myList"] as List)` I get a crash `Object reference not set to an instance of an object` – Carlos Rodrigez Apr 23 '17 at 16:58
  • You have not a List saved in your Property. You have a "String". So you have to deserialize the String to a List then do the "foreach" – Alessandro Caliaro Apr 23 '17 at 17:00
  • You have to deserialize JSON string to List, something like JsonConvert.DeserializeObject>(Application.Current.Properties["myList"]) – EvZ Apr 23 '17 at 17:01
  • awesome! thanks for the help guys! I did this to deserialize the json: `var data = JsonConvert.DeserializeObject>(Application.Current.Properties["myList"].ToString());` and it seems to work very well – Carlos Rodrigez Apr 23 '17 at 17:07
  • I kept losing my stored properties and this was the reason why. The documentation should just mention what types of objects you can store so you can't make this mistake. I was just storing a simple class with strings and integers. No list or whatever. – Martin Aug 30 '19 at 10:12