1

I'm using Xamarin.Essentials to store user preferences for my application. I have a scenario where I want to store multiple preferences which I'm using "sharedName" for (see code below).

    const string bShare = "BreakfastPrefs";
    private bool _eggs;
    private bool _bacon;

    public bool Eggs
    {
        get
        {
            //load from prefs
            return _eggs= Preferences.Get("EggSwitch", false, bShare );
        }
        set
        {
            //save to prefs
            _eggs= value;
            Preferences.Set("Eggswitch", value, bShare );
        }
    }

    public bool Bacon
    {
        get
        {
            //load from prefs
            return _bacon= Preferences.Get("BaconSwitch", false, bShare );
        }
        set
        {
            //save to prefs
            _bacon= value;
            Preferences.Set("BaconSwitch", value, bShare );
        }
    }

I was hoping that later on I could do something like 'Preferences.GetByShareName("bShare") to retrieve all preferences within that share. But it appears that I'm only to get each object back from the preferences one by one. I'm not really sure of the purpose of the shareName property of Preferences if I can't do this.

Here's an example of one of the Get method from Xamarin.Essentials.Preferences.

public static string Get(string key, string defaultValue, string sharedName);

Any help would be appreciated, the only other thing I can think of doing would be to store my Eggs and Bacon within one 'preference' as a delimited list, then when I want to retrieve it split it into a collection....but that feels a bit nasty.

Any input is appreciated. Thanks!

MisterZeeba
  • 95
  • 1
  • 5
  • What would you expect from `Preferences.GetByShareName("bShare")` to return exactly if there was one? – Pharaz Fadaei Oct 24 '21 at 09:49
  • A collection with one item, by using "GetShareByName" I think it would be fair for Preferences to return a collection regardless of the count – MisterZeeba Oct 24 '21 at 10:09
  • I still don't understand your expectations. For instance, if both `Eggs` and `Bacon` properties have been set to true, what would you expect as the result of the method? A list containing "EggSwitch" and "BaconSwitch"? – Pharaz Fadaei Oct 24 '21 at 10:19
  • you could manually serialize your settings and store them using "sharedName" as a key. You could modify the existing preferences (Essentials is open source) to suit your needs. Or you could just roll your own custom implementation - Preferences is a fairly simple KeyValue store. – Jason Oct 24 '21 at 12:16
  • Yes Pharaz, I'm sorry but I don't see what's wrong with that? a collection of Preference objects which contain the name and value of each item within that sharedName collection. – MisterZeeba Oct 24 '21 at 15:32
  • @Jason Thank you. Perhaps I need to look into extending Essentials to see if I can achieve what I'm after. – MisterZeeba Oct 24 '21 at 15:33
  • 1
    Looks like unfortunately my answer may be to store the object as JSON. See https://stackoverflow.com/questions/54903226/how-can-i-store-lists-in-xamarin-forms – MisterZeeba Oct 24 '21 at 16:12
  • I’m voting to close this question because this is philosophical question of what is the purpose of API if it doesn't work as OP expects. – Ivan Ičin Oct 24 '21 at 17:18
  • @MisterZeeba - imho, once you have successfully used json serialize/deserialize once, its an easy approach to storing/retrieving data such as this. It is often used with a web server, for data sent between server and client. – ToolmakerSteve Oct 25 '21 at 00:19

2 Answers2

0

The sharedName parameter is not supposed to be used as a way to categorize your internal preferences. Its purpose is to enable sharing of preferences OS-wide so that an application can use preferences set by another application.

As for categorizing your preferences, if your requirements are simple, you can serialize your values to store a string with your bShare as the key without specifying a sharedName. Otherwise, I suggest using an actual local database instead.

Pharaz Fadaei
  • 1,605
  • 3
  • 17
  • 28
0

For anyone interested, I solved this by serializing my own Dictionary object which stores the key and value as JSON. This is then stored within a Xamarin.Essentials.Preferences object. I then deserialize as required.

Thanks for the input.

MisterZeeba
  • 95
  • 1
  • 5