Windows 8–Storing App Data

While your Windows 8 Store application is running, you want to be saving data all the time.  After all, the user can switch to another application at any time, and you only have five seconds to store out your data at that point. 

While that is true for major data points, there are certain “status” values that you want to store only at the last minute.  This might include which information is visible, what is selected, user input data, and so forth.

A great way to store that information is in an ApplicationDataContainer.  This can be stored with your local settings or  your roaming settings, but it requires no special permission from the operating system; these files are considered safe. 

To see this, let’s create an application that stores, restores and deletes application data.  Our UI is very simple, a text box to put information into, a textBlock to display recalled information, and three buttons:

<StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="100">
   <StackPanel Orientation="Horizontal">
      <TextBlock Text="Settings"
                 Margin="5" />
      <TextBox Width="100"
               Height="30"
               Name="xSettings"
               Margin="5"/>
      <TextBlock Text="Setting: " Margin="5"/>
      <TextBlock Name="xSettingOutput"
                 Text="" 
                 Margin="5"/>
   </StackPanel>
   <Button Name="xSaveSettings"
           Content="Save"
           Click="xSaveSettings_Click_1" />
   <Button Name="xRetrieveSettings"
           Content="Retrieve"
           Click="xRetrieveSettings_Click_1" />
   <Button Name="xDeleteSettings"
           Content="Delete"
           Click="xDeleteSettings_Click_1" />

</StackPanel>

The event handlers are very straight forward. When you click Save we get the settings from the TextBox and write it into the settings collection, and then we blank the text box:

private void xSaveSettings_Click_1( object sender, RoutedEventArgs e )
{
   string userValue = xSettings.Text;
   settings.Values["UserSetting"] = userValue;
   xSettings.Text = String.Empty;
   
}

Retrieving the text is equally simple, we pull the text out of the settings collection as an object, and if it is not null we turn it into a string,

private void xRetrieveSettings_Click_1( object sender, RoutedEventArgs e )
{
   object val = settings.Values["UserSetting"];
   if ( val != null )
   {
      xSettingOutput.Text = val.ToString();
   }

}

Finally, deleting a setting requires just a call to the Remove method,That’s all it takes to manage your application state.  Piece of cake. Easy as pie.

 private void xDeleteSettings_Click_1( object sender, RoutedEventArgs e )
 {
    settings.Values.Remove( "UserSetting" );
    xSettingOutput.Text = String.Empty;
 }


Share

About Jesse Liberty

Jesse Liberty is a Master Consultant for Falafel Software, and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Technical Evangelist for Telerik and for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS.
This entry was posted in Essentials, Mini-Tutorial, Windows 8 and tagged . Bookmark the permalink.

5 Responses to Windows 8–Storing App Data

  1. testdomain says:

    Incredible points. Great discussions. Maintain the amazing work.

  2. Pingback: The Daily Six Pack: February 14, 2013 | Dirk Strauss

  3. Pingback: Windows Store Developer Links – 2013-02-11 | Dan Rigby

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>