Floating FB popout byF5debug

Handling data in Windows Store Application – Day 12

 

imageIn this article we are going to see how to handle data in your Windows Store application development. In our previous article we have seen what are contracts in windows store and how to use the different contracts that are available with the development of Windows Store. Handling data in any application is a challenge, if we need to keep the data secure and what data should be accessible by the application on what stages, how to manage the settings data and user data etc. These are some of the questions that comes into our mind while developing an application. So in this article we will see how we can handle the data easily and the different options that are available while developing the application.       [more]


Handling data in Windows Store application can be performed in different ways as to which data to store and where to store. Microsoft recently launched a new services on the cloud which is Windows Azure Mobile Services which is specifically for data handling purposes on the cloud so that the data will be securely stored over the cloud and can be access from multiple clients in different formats. But if we don’t want to use these find of a service and want to use a traditional method below are some of the options available.

  • Local - Uses to store the data that exists only on to the specific device the app is running
  • Roaming - Data will be available on all the devices where the application is installed
  • Temporary - Data stored in a local temporary storage and will be removed when app is not running

Let us see the approach on how to use one of these types and do some operations of data in our application. Open Visual Studio 2012 IDE in administrator mode and create a new Windows Store application with C# and XAML with a valid project name as shown in the screen below.

 image


Now let us add some controls to demonstrate the process of storing the data to the local machine. We will add a textbox to get the user input and a button control to trigger an event to store the user data to the local storage and another button to retrieve the stored data as shown in the screen below.

image


XAML Code:

[code:c#]

<Page
    x:Class="HandlingDataSample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HandlingDataSample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Save Data" FontSize="20" HorizontalAlignment="Left" Margin="304,197,0,0" VerticalAlignment="Top" Height="75" Width="214" Click="Button_Click_1"/>
        <TextBox x:Name="txtEnter" HorizontalAlignment="Left" Margin="304,67,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="83" Width="464"/>
        <TextBlock HorizontalAlignment="Left" FontSize="30" Margin="135,90,0,0" TextWrapping="Wrap" Text="Enter Data" VerticalAlignment="Top" Height="41" Width="164"/>
        <Button Content="Read Data" FontSize="20" HorizontalAlignment="Left" Margin="554,197,0,0" VerticalAlignment="Top" Height="75" Width="214" Click="Button_Click_2"/>
        <TextBlock x:Name="txtOutput" HorizontalAlignment="Left" Margin="304,319,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="74" Width="464"/>

    </Grid>
</Page>

[/code]

Let us write the code which does the logic to create a storage container locally and create a file to store the data which we enter as user inputs. Windows.Storage namespace is used to get these container and file references to create an instance and use it as shown in the code below.

C# Code:

[code:c#]

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace HandlingDataSample
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        const string filename = "F5debugFile.txt";
        StorageFolder sFolder = null;

        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            sFolder = ApplicationData.Current.RoamingFolder;
            StorageFile file = await sFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteTextAsync(file, "Welcome F5debug - " + txtEnter.Text.ToString());
            txtOutput.Text = "File Saved Locally";
        }

        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            StorageFile strfile = await sFolder.GetFileAsync(filename);
            string strreadfile = await FileIO.ReadTextAsync(strfile);

            txtOutput.Text = "File Content is - " + strreadfile;
        }
    }
}

[/code]

image


Here in the code we can specify RoamingFolder or LocalFolder or TemporaryFolder which can be used based on the requirement. In this sample we will use the Roaming folder option so that the data will be access across the devices, basically select the appropriate option to get use of the data. In the second button click event we get access to the file asynchronously and read through the content to display the data to the user.  Build and execute the application and we can see the expected output as shown in the screens below.

image

image

image


Conclusion:

In this article we have seen how to manipulate and store the data locally or remotely with the inbuilt properties available with the SDK. In real time we have the different option to explore more and effectively use it to use the application data effectively as and when required. 

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter