2021.02.04.

Xamarin.Forms save and read objects

By bence960206

Download this Package to your .NET Standard Library: https://www.nuget.org/packages/Xam.Plugins.Settings/

If you don’t downloaded the Newtonsoft.Json Package yet, please add this package too.

After the installations, we can save and read any object in your application:

Save:

Plugin.Settings.CrossSettings.Current.AddOrUpdateValue("CurrentTask", JsonConvert.SerializeObject(yourobject));

Read:

var objectInJSON = Plugin.Settings.CrossSettings.Current.GetValueOrDefault("CurrentTask", "");
JsonConvert.DeserializeObject<yourobject>(objectInJSON)

Hints: If you get some error during de serialization, you may can’t serialize your object because of some property (Xamarin.Forms.Color, View, or any Bindible object, etc…). Because of this error, you have to create a new class with the same property list, but do not use more complexed classes, but use sample data types: string, List, bool, etc…