|
原文地址:http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-15-Isolated-Storage.aspx
本文讨论Windows phone 7内部的数据存储方式。
什么是独立存储?
独立存储不是一个新鲜的事物。他在Silverlight2中已经被使用了。他是在本地文件系统中保存数据文件的一种基本方式,说他独立是因为只有你的程序能够接触到这些数据,如果你有两个程序,并且想在相互之间共享数据,那你最好把数据放到云端。程序没有方法在本地进行数据交互。
设置以及文件
有两种方式来本地保存数据。一种方式是通过Key/Value保存,我们称其为独立存储设置。第二种方式是实际保存文件或是创建文件夹。这叫做独立存储文件。下图是对两种方式方式的简单阐述:
独立存储设置
出于很多原因,这也许是你唯一的选择。独立存储设置运行你以字典的方式保存有用的数据。他将会在程序启动或是没电的情况下以及存在,除非你卸载了程序。还有一点就是在添加到库之前不能够检索值。在我的示例中,你会看到我在检索值之前首先是看其是否存在。下面是保存用户的邮件偏好的示例程序。我有一个选择框,允许用户设置:
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 Day15_IsolatedStorage
{
public partial class MainPage : PhoneApplicationPage
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
// Constructor
public MainPage()
{
InitializeComponent();
InitializeSettings();
}
private void InitializeSettings()
{
if (settings.Contains("emailFlag"))
{
EmailFlag.IsChecked = (bool)settings["emailFlag"];
}
else settings.Add("emailFlag", false);
}
private void EmailFlag_Unchecked(object sender, RoutedEventArgs e)
{
settings["emailFlag"] = false;
}
private void EmailFlag_Checked(object sender, RoutedEventArgs e)
{
settings["emailFlag"] = true;
}
}
}
正如你所见到的,这十分简单,但是需要记住几件事情:
1 在未被创建之前检索值将会引发错误。要记住初始化设置或是一只检测.Contains属性值。
2 你可以保存任何想要保存的值。我在示例中保存的是boolean类型,但是同样可以保存用户自定义类型或是任何你想到的类型。
3 在使用之前必须进行类型转换。你注意到我在检索之前将数据类型转换为bool类型。虽然你保存了数据,但是存储器不知道你保存的类型,这是你需要自己实现的。
4 设置一个值和添加值一样。"setting.Add()"不是必须的,但是我添加进来让你能够清楚语法。
独立存储文件
使用独立存储文件能够让你在用户的机器上实际保存文件。示例中,我将会在二级文件夹下创建一个文本文件,然后读取内容。我们将能够创建文件夹、二级文件夹以及文件。感觉需要很多代码,其实很简单,我们将会创建爱你一个IsolatedStorageFile对象,然后将其写入IsolatedStorageFileStream,我在代码中添加了一些注释,所以你将会更容易理解。我写了连个事件,保存与读取:
using System.IO.IsolatedStorage;
using System.IO;
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create new subdirectory
fileStorage.CreateDirectory("textFiles");
//Create a new StreamWriter, to write the file to the specified location.
StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.OpenOrCreate, fileStorage));
//Write the contents of our TextBox to the file.
fileWriter.WriteLine(writeText.Text);
//Close the StreamWriter.
fileWriter.Close();
}
private void GetButton_Click(object sender, RoutedEventArgs e)
{
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new StreamReader
StreamReader fileReader = null;
try
{
//Read the file from the specified location.
fileReader = new StreamReader(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.Open, fileStorage));
//Read the contents of the file (the only line we created).
string textFile = fileReader.ReadLine();
//Write the contents of the file to the TextBlock on the page.
viewText.Text = textFile;
fileReader.Close();
}
catch
{
//If they click the view button first, we need to handle the fact that the file hasn't been created yet.
viewText.Text = "Need to create directory and the file first.";
}
}
这些看起来并没有什么神奇,但是当你离开程序再回来并且重新恢复以前的状态以后才会真正发现他的神奇。
你现在有两种方式来创建独立存储。更多参见MSDN |
|