65.9K
CodeProject is changing. Read more.
Home

Isolated Storage in Windows Phone 8

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.70/5 (6 votes)

Dec 19, 2013

CPOL
viewsIcon

17181

downloadIcon

262

This tip shows you how to use the Isolated Storage in Windows Phone 8.

Introduction

This tip shows you how to use the Isolated Storage in order to store and retrieve Application Settings like the name or the Identifier of the user of the current application.

Using the Code

Before we start coding, we must include System.IO.IsolatedStorage.

using System.IO.IsolatedStorage; 

Together, we will build an application where the user puts a number and a string.

<Grid x:Name="ContentPanel" Margin="24,133,0,28" Grid.RowSpan="2">
    <TextBlock HorizontalAlignment="Left" Margin="75,20,0,0" 
       TextWrapping="Wrap" VerticalAlignment="Top" 
       RenderTransformOrigin="0.231,0.624">
        	<Run Text="Give  a number"/>
        	<Run Text=":"/>
    </TextBlock>
    <TextBox x:Name="Entier" HorizontalAlignment="Left" Height="72" 
      Margin="63,52,-63,0" TextWrapping="Wrap" Text="" 
      VerticalAlignment="Top" Width="456"/>
    <TextBlock HorizontalAlignment="Left" Margin="75,155,0,0" 
      TextWrapping="Wrap" VerticalAlignment="Top">
        	<Run Text="Give a string"/>
        	<Run Text=": "/>
    </TextBlock>
    <TextBox x:Name="chaine" HorizontalAlignment="Left" 
      Height="72" Margin="63,205,-63,0" TextWrapping="Wrap" 
      Text="" VerticalAlignment="Top" Width="456"/>

    <Button Content="save " HorizontalAlignment="Left" Margin="63,299,0,0" 
      VerticalAlignment="Top" Click="Button_Click_1" 
      RenderTransformOrigin="0.461,-2.292"/>
    <Button Content="put" HorizontalAlignment="Left" 
      Margin="234,299,0,0" VerticalAlignment="Top" Click="Button_Click_2"/>
</Grid>

Store Application Settings

If we want to store the number and the string entered by the user, we click upon a buton called "PUT" as shown in the following example:

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
    int integerValue;
    int.TryParse(Entier.Text, out integerValue);
    //store the integer
    isoStoreSettings["IntegerValue"] = integerValue;
    //store the string
    isoStoreSettings["StringValue"] = chaine.Text;
} 

Retrieve Application Settings

Now we want to retrieve the application settings, that is why we click upon the button "PUT"which has the following code:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    IsolatedStorageSettings isoStoreSettings = IsolatedStorageSettings.ApplicationSettings;
    int integerValue;
    string stringValue;
    //Retreive The integer
    if (isoStoreSettings.TryGetValue("IntegerValue", out integerValue))
    {
        Entier.Text = integerValue.ToString();
    }
    //Retreive the string
    if (isoStoreSettings.TryGetValue("StringValue", out stringValue))
    {
        chaine.Text = stringValue;
    }
    isoStoreSettings.Save();
} 

Any comments are welcome.