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"
      <TextBlock Text="Setting: " Margin="5"/>
      <TextBlock Name="xSettingOutput"
   <Button Name="xSaveSettings"
           Click="xSaveSettings_Click_1" />
   <Button Name="xRetrieveSettings"
           Click="xRetrieveSettings_Click_1" />
   <Button Name="xDeleteSettings"
           Click="xDeleteSettings_Click_1" />


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;

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Essentials, Mini-Tutorial, Windows 8 and tagged . Bookmark the permalink.