Logo
HOW TO
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Phone

Windows Phone 7 : Isolated Storage - Modifying Settings of Your Application

7/4/2012 4:22:16 PM

1. Problem

You want to locally save some settings about your application and then retrieve those settings later.

2. Solution

You must use the IsolatedStorageSettings class to access your application settings adding and deleting keys, like a dictionary.

3. How It Works

IsolatedStorageSettings enables you to easily and locally store user-specific data as key/value pairs in the object IsolatedStorageFile. You will see that the use of IsolatedStorageSettings is equal to the use of a dictionary <TKey, TValue>.

4. The Code

A type of push notification is toast notification . that requires that your application to be accepted and published on Marketplace must ask users whether they want to be notified with them. What better place for your application to store the user's choice than in the application settings?

To create this recipe, we have not focused on the application's user interface, but on the page of settings. Suppose that our application use notifications and sounds and we want to give to your user the possibility to disable or enable them

...
<phone:PhoneApplicationPage.ApplicationBar>
  <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
    <shell:ApplicationBarIconButton x:Name="SaveSettingsButton"
      IconUri="/icons/appbar.save.rest.jpg"
      Text="Save" Click="SaveSettingsButton_Click" />
  </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

<Grid x:Name="LayoutRoot" Background="Transparent">
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
  </Grid.RowDefinitions>

  <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="PageTitle" Text="Settings" Style="{StaticResource
      PhoneTextNormalStyle}"/>
  </StackPanel>
  <ScrollViewer x:Name="ContentPanel" Margin="12,0,12,0" Grid.Row="1" >
    <StackPanel>
      <toolkit:ToggleSwitch Header="Toast Notification" x:Name="ToastSwitch"  />
      <toolkit:ToggleSwitch Header="Sounds" x:Name="SoundsSwitch" />
    </StackPanel>
  </ScrollViewer>

					  

This XAML, inserted into the template of your page, will show an interface similar to that in Figure 1. We say similar because you might not be using the same icon or the same control to check the settings. For example, you could have chosen a check box or toggle button.

NOTE

The control ToggleSwitch is contained in the Windows Phone controls toolkit, which is available on CodePlex.

Figure 1. The Settings page

In the code-behind, of the page Settings, within the constructor you subscribe to the loaded event with Settings_Loaded event handler:

public Settings()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Settings_Loaded);
        }

And you define two class-level constant strings:

...
private const string _toastEnabledKey = "ToastEnabled";
private const string _soundsEnabledKey = "SoundsEnabled";
...

Why do you do this? Because basically, by accessing settings through a dictionary, you will have a number of key/value pairs that could potentially include everything.

The event handler Settings_Loaded will retrieve your settings and change the value of the switch with the following code:

void Settings_Loaded(object sender, RoutedEventArgs e)
{
  if (IsolatedStorageSettings.ApplicationSettings.Contains(_toastEnabledKey))
  {
    var toast = IsolatedStorageSettings.ApplicationSettings[_toastEnabledKey].ToString();
    this.ToastSwitch.IsChecked = bool.Parse(toast);
  }
  if (IsolatedStorageSettings.ApplicationSettings.Contains(_soundsEnabledkey))
  {
    var sounds = IsolatedStorageSettings.ApplicationSettings[_soundsEnabledkey].ToString();
    this.SoundsSwitch.IsChecked = bool.Parse(sounds);
  }
}

					  

Line by line, what happens is as follows:

  1. If the dictionary contains the key that you have associated with the settings for the toast notification,

    1. Recover the value from ApplicationSettings (as with a normal dictionary), getting it as a string (in fact, the type of value is an object ).

    2. Assuming you have a Boolean value, you just do the Parse. If you can't be sure about what you will (God save you), you might opt for a tryparse. Finally, you associate the parsed value to the IsChecked property of the switch.

  2. Repeat the preceding step for the key that relates the sounds.

In this way, you have loaded the settings related to your application, and so now you can see how to save them, with the handler SaveSettingsButton_Click:

private void SaveSettingsButton_Click(object sender, EventArgs e)
{
  IsolatedStorageSettings.ApplicationSettings[_toastEnabledKey] =
     this.ToastSwitch.IsChecked;
  IsolatedStorageSettings.ApplicationSettings[_soundsEnabledkey] =
     this.SoundsSwitch.IsChecked;
}

The only thing to do to save a setting of the dictionary is assign a key and a value, then thinks at all the class IsolateStorageSetting. In order to pursue this exercise further, consider at this point using the settings to save the BPM of the metronome of our application 7Drum, for example.

5. Usage

Start the toast notification application by pressing F5 from Visual Studio. Open the Settings page. Change the settings and save them, thereby provoking the tombstoning of this application. Start the application again and open the Settings page to see that your settings have been saved.

Other -----------------
- Windows Phone 7 : Isolated Storage - Saving Serialized Data
- Windows Phone 7 : Saving a File in Isolated Storage and Loading It
- Windows Phone 7 : Media Management - Adding Integration with the Music-Videos Hub
- Windows Phone 7 : Sounding Out with Game Audio - Playing Music
- Windows Phone 7 : Playing Sound Effects
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Adding Artificial Intelligence
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Adding 100 Killer Tangerines
- Windows Phone 7 : Using the Microphone in the Funny Repeater Application
- Windows Phone 7 Advanced UI Development : The Microsoft Advertising SDK
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Constructing Class Instances
 
 
REVIEW
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox
 
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
 
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
Top 10
- Microsoft Excel : How to Use the VLookUp Function
- Fix and Tweak Graphics and Video (part 3) : How to Fix : My Screen Is Sluggish - Adjust Hardware Acceleration
- Fix and Tweak Graphics and Video (part 2) : How to Fix : Text on My Screen Is Too Small
- Fix and Tweak Graphics and Video (part 1) : How to Fix : Adjust the Resolution
- Windows Phone 8 Apps : Camera (part 4) - Adjusting Video Settings, Using the Video Light
- Windows Phone 8 Apps : Camera (part 3) - Using the Front Camera, Activating Video Mode
- Windows Phone 8 Apps : Camera (part 2) - Controlling the Camera’s Flash, Changing the Camera’s Behavior with Lenses
- Windows Phone 8 Apps : Camera (part 1) - Adjusting Photo Settings
- MDT's Client Wizard : Package Properties
- MDT's Client Wizard : Driver Properties
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro