Skip to content

βš™οΈ Preferences

Marc Rousavy edited this page Aug 22, 2018 · 3 revisions

An abstract class definition for any application preferences.

Create a new class that will hold your app preferences which inherits from the Preferences class:

public class DemoPreferences : Preferences
{
    public int SomeInt { get; set; } = 400;
    public string SomeString { get; set; } = "test string";
    public bool SomeBool { get; set; } = false;

    public object SomeObject { get; set; } = new
    {
        Name = "Marc",
        IsValid = true
    };
}

Loading and saving is pretty trivial with the Preferences base:

  • Creating new Preferences
var prefs = new DemoPreferences();
Preferences.Save(prefs, Preferences.RecommendedPath);
  • Loading Preferences
var prefs = Preferences.Load<DemoPreferences>(Preferences.RecommendedPath);

Or Preferences.LoadOrDefault

The generated config.json file looks like this:

{
   "SomeInt":400,
   "SomeString":"test string",
   "SomeBool":false,
   "SomeObject":{
      "Name":"Marc",
      "IsValid":true
   }
}
Clone this wiki locally