Page 1 of 1

WP7 - Using IsolatedStorageSettings Rate Topic: -----

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 07 November 2010 - 12:59 PM

In a previous tutorial, I wrote about using the Isolated Storage to store files on a WP7 device. Let's say that instead of storing data, you wanted to store settings for your application. This where the IsolatedStorageSettings comes in.

The IsolatedStorageSettings is a place to store Key/Value pairs of data.

So let's say that my application used the Microsoft Ad control, but I wanted to be nice and give the user the ability to turn the ads on and off.

On my Settings screen, I would have a checkbox(AdsCheckbox) for allowing the ads. In code, I would save the value by doing this...

var settings = IsolatedStorageSettings.ApplicationSettings;

if (settings.Contains("AllowAds"))
    settings["AllowAds"] = AdsCheckbox.IsChecked.Value;
else
    settings.Add("AllowAds", AdsCheckbox.IsChecked.Value);

settings.Save();



If you will notice, the ApplicationSettings object does have a method that allows you to check to see if the key already exists in the Settings. An exception will be thrown if you try to add a key that already exists, so it's necessary to do this check before assigning a value.

Also, as you can see, it's very easy to get the value out of the settings...

// when the form opens, I want to set the Checkbox to being checked
//   or unchecked depending on the setting
var settings = IsolatedStorageSettings.ApplicationSettings;

if (settings.Contains("AllowAds"))
    AdsCheckbox.IsChecked = bool.Parse(settings["AllowAds"].ToString());



However, the IsolatedStorageSettings class isn't only for simply datatypes. It can also store your custom class objects.

Employee class
public class Employee
{
    public string FullName { get; set; }
    public decimal Salary { get; set; }
}



Then on my form, I have a button to save an instance of the Employee class to the IsolatedStorageSettings...

private void btnSave_Click(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    Employee emp = new Employee()
    {
        FullName = "John Doe",
        Salary = 250000
    };

    settings.Add("Employee1", emp);
    settings.Save();
}



I also have a button that will load the object from the IsolatedStorageSettings...

private void btnLoad_Click(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    Employee emp;

    if (settings.TryGetValue<Employee>("Employee1", out emp))
    {
         MessageBox.Show(string.Format("Full Name: {0}\nSalary: {1:c}", emp.FullName, emp.Salary));
    }
}



Notice that the IsolatedStorageSettings.ApplicationSettings class also has the TryGetValue method. This method behaves exactly like the TryParse methods of the .Net datatypes. It will return a boolean specifying whether the key was found and whether the conversion from object --> Employee was successful. If it's successful, it will return the instance as the out parameter.

After running the application, clicking the Save button, then clicking the Load button, we will get this message box...

Attached File  11-7-2010 2-55-20 PM.png (8.46K)
Number of downloads: 5

Is This A Good Question/Topic? 0
  • +

Replies To: WP7 - Using IsolatedStorageSettings

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Posted 07 November 2010 - 01:50 PM

In your experience working with this would you recommend using a strongly typed class for saving settings instead of arbitrary key/value pairs?

It seems that using a holder class would let you have strongly typed classes and avoid some bugs when typing some long key names. But then you'd have to cast the setting with the TryGetValue() method.

Thoughts?
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 07 November 2010 - 05:13 PM

For ease of use, I would probably use this. You could always use a constant that would hold the name of the setting in a globals class like..

public static class GLOBALS
{
     public static const EmployeeSettingName = "Employee1";
}



Then used like....

var settings = IsolatedStorageSettings.ApplicationSettings;

if (settings.Contains(GLOBALS.EmployeeSettingName))
    settings[GLOBALS.EmployeeSettingName] = emp;
else
    settings.Add(GLOBALS.EmployeeSettingName, emp);



EDITED: I must have misunderstood your question. I just saw your tutorial(which I will test and approve later), and I actually do that in my WP7 app.

So, yeah, writing a helper class that contains all of your settings as properties, then saving that class in the IsolatedStorageSettings is something that I actually do.
Was This Post Helpful? 1
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Posted 07 November 2010 - 05:52 PM

Cool, thanks for the input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1