-1

I am trying to save the state of some labels on an Xamarin.Forms App. I am following the steps of Creating Mobile Apps with Xamarin.Forms book, it shows how to store value per value but recommends this for when you have several info:

An application with many more items might want to consolidate them in a class named AppSettings (for example), serialize that class to an XML or a JSON string, and then save the string in the dictionary.

I am lost about how to do it, I created a simple class

class AppSettings
{
    public double? gain = null;
    public string mainTitle = "my title";
}

In my constructor I recover the properties in this way

      //! Recover appSettings if some

        IDictionary<string, object> properties = Application.Current.Properties;
        if (properties.ContainsKey("mainTitle"))
        {
            mainTitle.Text = properties["mainTitle"] as string;
        }

        //! New code for loading previous keypad text.
        App app = Application.Current as App;
        mainTitle.Text = app.MainTitle;

And the event that I use to save it is:

void onHomeOptionButtonClicked(object sender, EventArgs args)
        {

            Button buttonClicked = (Button)sender;

            mainTitle.Text =  buttonClicked.Text;
            // !Save keypad text.
            App app = Application.Current as App;
            app.MainTitle = mainTitle.Text;
        }

What I do not know is how to structure my Class so it can be serialized to JSON (I also do not know how to do this serialization)

I wanted to create a class in C# with this Javascript's idea:

myAppSettings = {
    HomePage : {
        name1 : "value1",
        name2 : "value2"
        name3 : {
            name3.name1 : "value3.1"
        }
    },
    OtherPage:{
        name1 : "value1",
        name2 : "value2"
    },
    .
    .
    .
};

Which is the correct way to do it in C#?

distante
  • 4,507
  • 3
  • 29
  • 63
  • Do you mean a javascript `object` or a `prototype` ? - because the C# equivalent of a javascript `object` is indeed a C# `object` (instance of a `class`), and the equivalent (with some significant differences) of a javascript `prototype` is a C# `class`, – Charles Bretana Dec 26 '16 at 17:43
  • Sorry, I re did the hole question as per mods recommendations.. – distante Dec 26 '16 at 18:28
  • @distante Now your question is good. All you need is to put all your setting values to a class and then serialize it as `JsonConvert.SerializeObject(yourSetting)`. See http://www.newtonsoft.com/json – L.B Dec 26 '16 at 18:33
  • If all you are concerned with is being able to serialize an instance of a C# class to a json string, then I believe the json serializer/deserializer in json.Net (http://www.newtonsoft.com/json/help/html/SerializingJSON.htm) will do that for any C# object. – Charles Bretana Dec 26 '16 at 18:34
  • Is this helpful ? http://stackoverflow.com/questions/31655327/how-can-i-save-some-user-data-locally-on-my-xamarin-forms-app – Zein Makki Dec 26 '16 at 18:37
  • @L.B that is one part, the other one is how to correctly structure this type of class. Thanks! – distante Dec 26 '16 at 19:15
  • @distante any class can work. Just use a class that is suitable for your needs... – L.B Dec 26 '16 at 19:26

1 Answers1

1

If all you are concerned with is being able to serialize an instance of a C# class to a json string, then I believe the json serializer/deserializer in json.Net will do that for any C# object.

Charles Bretana
  • 131,932
  • 22
  • 140
  • 207
  • I do not quite understand, is Json.net open source or it costs? I am not working yet with C#/Xamarin but I am learning it, but I would like to not depend of a expensive extension ($599) **Edit** I saw, it has an MIT licence and the $599 are for support. – distante Dec 26 '16 at 21:15
  • Json.NET is open source under the MIT license and is free for commercial use. – Charles Bretana Dec 27 '16 at 02:20