I wrote a little sample application that lets the user work with the isolated storage on a Windows Phone 7 device. I won’t bother you with the details of the xaml because the UI is relatively straight forward. There are only a handful of controls and they are all pretty standard. What I want to share here is the code behind that makes this application work. Besides, it’s not really the most amazing app out there, it’s just meant to show you how to work with ISO storage.
The key take-aways from this are:
1) You will need to add reference to the System.IO.IsolatedStorage assembly and add the corresponding using statement.
2) You save and retrieve the settings by calling IsolatedStorageSettings.ApplicationSettings
3) You access the setting values via their keys - IsolatedStorageSettings.ApplicationSettings[“key”]= “value”;
Hopefully this gets you started saving and retrieving data to and from a Windows Phone 7 device.
More to come… happy coding!
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;
using System.IO;
namespace Isolated_Storage
{
public partial class MainPage : PhoneApplicationPage
{
private IsolatedStorageSettings appSettings;
public MainPage()
{
InitializeComponent();
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
btnAdd.Click += new RoutedEventHandler(btnAdd_Click);
btnRead.Click += new RoutedEventHandler(btnRead_Click);
txtValue.TextChanged += new TextChangedEventHandler(txtValue_TextChanged);
appSettings = IsolatedStorageSettings.ApplicationSettings;
UpdateSettingsCount();
}
private void UpdateSettingsCount()
{
lblCount.Text = String.Format("App Settings Count: {0}", appSettings.Count);
}
void txtValue_TextChanged(object sender, TextChangedEventArgs e)
{
btnAdd.IsEnabled = txtValue.Text.Trim().Length > 0;
}
void btnRead_Click(object sender, RoutedEventArgs e)
{
lstItems.Items.Clear();
LoadData();
}
void btnAdd_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings.ApplicationSettings[(appSettings.Count + 1).ToString()] = txtValue.Text;
UpdateSettingsCount();
}
private void LoadData()
{
for (int i = 0; i < appSettings.Count; i++)
{
string entry = string.Format("Key: {0} {1}Value: {2}", i + 1, Environment.NewLine, appSettings[(i + 1).ToString()].ToString());
lstItems.Items.Add(entry);
}
}
}
}
can hav a zip code copy of this?
ReplyDeletePlease post the source code. Thank you.
ReplyDelete