Working with Isolated Storage’s in Windows Phone 7
Join the DZone community and get the full member experience.
Join For FreeIn this article we are going to see what is Isolated Storage in Windows Phone 7 Development. Isolated Storage in Windows Phone 7 is something similar to what we have with Silverlight. In Windows Phone 7 Isolated Storage is space provided to store the data locally which is isolated from the other applications. Each application can have their own space(storage) so that other application data cannot be shared with in the application which provides greater data security. In case we need to share the data, we can have Azure storages or any other cloud medium to share the data between the applications.
In Windows Phone 7 Development Isolated Storage can be used in 3 different ways to store the data like Storing data like Key-Value pair, Second type is using the Files and Folders to store the data in separate files, and lastly using the Relational data by using the local database. In Windows Phone 7 Architecture, we have all the files and folders are limited with access to the specific application itself by using the Isolated storage, which avoid the application to access other application data and vice versa. This architecture avoid data threat and provides security for the un authorised access to the data. Out of the three type of data storage we have to store in Isolated Storage File and Folder plays a vital role in most of the Windows Phone 7 Application development since we need to persist the user data locally in order to use it as and when required for online and offline mode.
In this article we will see how to use the Key Value Pair option to store and retrieve the data using the Isolated Storage, this option uses the IsolatedStorageSettings Class to store the data. IsolatedStroageClass provides an effective way to store the data in Key Value pairs in a local IsolatedStorageFile. In Windows Phone 7 Application development, IsolatedStorageSettings is not thread safe so the application throws an IsolatedStorageException when the data Save is called. IsolatedStorageSetting class normally uses the below 3 methods to store and retrieve the data with the isolated storage.
- Add – We can make use of this method to store the data in the dictionary as key value pair.
- Remove – We can make use of this method to remove/delete the data with a specified key.
- Contains – We can make use of this method to check if particular data is available or not using the specified key.
Let us now see the step by step process on how to use the IsolatedStorageSetting class to store the data in a key value pair.
Steps:
Open Visual Studio 2010 IDE and Create a new Silverlight for Windows Phone 7 Application with a valid project name as shown in the screen below.
Now let us add some control to get the user input and save it to the
Isolated Storage. Also we will add some buttons to retrieve the data
using the key and also we will add one more button to delete the data
using the same key. So once we add our controls, we can see the page
design as shown in the screen below.
XAML Code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Height="39" HorizontalAlignment="Left" Margin="12,67,0,0" Name="textBlock1" Text="Data" VerticalAlignment="Top" Width="106" TextAlignment="Center" /> <TextBox Height="72" HorizontalAlignment="Right" Margin="0,42,17,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="347" /> <Button Content="Retrive" Height="72" HorizontalAlignment="Left" Margin="59,244,0,0" Name="button1" VerticalAlignment="Top" Width="160" /> <Button Content="Delete" Height="72" HorizontalAlignment="Left" Margin="225,244,0,0" Name="button2" VerticalAlignment="Top" Width="160" /> <Button Content="Save Data" Height="72" HorizontalAlignment="Left" Margin="59,166,0,0" Name="button3" VerticalAlignment="Top" Width="326" /> <TextBlock Height="204" HorizontalAlignment="Left" Margin="73,355,0,0" Name="textBlock2" Text="" VerticalAlignment="Top" Width="299" /> </Grid>
First we need to add the Using directive to get the Isolated Storage
class which is used to do the manipulations with the application. So
copy the below Using directive to the code behind page.
using System.IO.IsolatedStorage;
Now let us start writing our code to use the IsolatedStorageSettings
class, first we need to create an instance of the class and try to
invoke the ADD Method to save the data. Copy the below code to the Save
Data button control event as shown in the screen below.
Code Behind:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using System.IO.IsolatedStorage; namespace F5debugWp7IsolatedStorage { public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); } private void button3_Click(object sender, RoutedEventArgs e) { IsolatedStorageSettings ISSetting = IsolatedStorageSettings.ApplicationSettings; if (!ISSetting.Contains("DataKey")) { ISSetting.Add("DataKey", txtSaveData.Text); } else { ISSetting["DataKey"] = txtSaveData.Text; } ISSetting.Save(); } } }
In the above code we can see we have a key DataKey which is used to
save the data which the user types in the text block. First we need to
create an instance of the IsolatedStorageSettings and
uses the instance to call the ADD method by passing the Key and the
Value. Here we have provided with some validation to first check if the
Key (DataKey) is already available or not if the data key is not
available we make use of the SAVE Method to save the data to the
Isolated Storage. Now let us write the code to retrieve the data using
the key which we saved in the above code. To retrieve the data copy the
below code the Retrieve button click event. To retrieve the data the key
value pair is accessed directly from the applicationsettings property
if the IsolatedStorageSettings as shown in the screen below.
Code Behind:
private void button1_Click(object sender, RoutedEventArgs e) { if (IsolatedStorageSettings.ApplicationSettings.Contains("DataKey")) { textBlock2.Text = "Key : DataKey || Value : "; textBlock2.Text += IsolatedStorageSettings.ApplicationSettings["DataKey"] as string; } else { textBlock2.Text = "Key Not Found"; } }
Finally let us write the code to delete the data using the key
(DataKey), here also we need to use the ApplicationSettings Property to
access the data using the Key and delete it using the Remove method as
shown in the screen below.
Code Behind:
private void button2_Click(object sender, RoutedEventArgs e) { if (IsolatedStorageSettings.ApplicationSettings.Contains("DataKey")) { IsolatedStorageSettings.ApplicationSettings.Remove("DataKey"); } }
Now we are done with the code, we can build and execute the project
by simply pressing the F5 button from the keyboard or use the Tool bar
to select the Build and Execute solution. Once the application is build
successful we can see the application opened in the Windows Phone 7
Emulator as shown in the screen below. We can play around with the
application by adding the data and retrieving the data using the key
which we assigned internally.
Output Screens:
Conclusion:
So in this article we have seen what Isolated Storage is in Windows Phone 7 Development and we have seen how to use the Key Value Pairs to save and retrieve the data effectively. In our upcoming tutorials we will see how to use the Files and Folders to save the data in the Isolated Storage.
Thanks for reading my article. If you like my blog and if you are
interested in getting the latest updates on new articles, kindly follow
me through one of these options.
Published at DZone with permission of Karthikeyan Anbarasan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments