Telerik blogs

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;
}

Download the source code


Win8_Download (2)


jesseLiberty
About the Author

Jesse Liberty

 has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and has been a Distinguished Software Engineer for AT&T and a VP for Information Services for Citibank and a Software Architect for PBS. You can read more on his personal blog or follow him on twitter

Comments

Comments are disabled in preview mode.