How to Store Lists in Xamarin Forms Settings Plugin

abvabv USMember ✭✭✭

Hi, can anyone please help me for how to Store Lists in Xamarin Forms Settings Plugin.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Refer to the documentation of Setting Plugin, we should know List is out of its scope.
    If you do want to store a List or other objects, I recommend you to convert it to json string first before storing. Then retrieve the string and deserialize it to List.

    // Store
    List<Model> list = new List<Model>();
    for (int i=0; i<10; i++)
    {
        list.Add(new Model { Title = "Item" + i });
    }
    var listString = JsonConvert.SerializeObject(list);
    
    CrossSettings.Current.AddOrUpdateValue("Key", listString);
    
    // Retrieve
    string listString = CrossSettings.Current.GetValueOrDefault("Key", string.Empty);
    List<Model> list = JsonConvert.DeserializeObject<List<Model>>(listString);
    

    For using this JsonConvert, you have to install the Newtonsoft.Json.

  • abvabv USMember ✭✭✭

    thanks @LandLu

  • LandLuLandLu Member, Xamarin Team Xamurai

    @AsV If you felt that helpful, you could accept that as an answer to finish this discussion.

Sign In or Register to comment.