SL_Isolate Storage的使用
作者:austinleng
在SL开发中,你可能需要使用虚拟文件系统,分配一定的存储空间来保存一些数据,SL提供了Isoloate Store,也就是隔离存储来实现这个需求,具体可看微软微软的官方解释为。
IsolateStorage
using System.IO.IsolatedStorage;
Published Sunday,March 08,2009 10:01 PM by AustinLG
主要的类有:
IsolatedStorageSettings; //以Dictionary<<(TKey, TValue)>> 键值对的方式来存储数据,表面上不直接进行IO,
//但实质依然是IO的操作,且占用物理空间,可以通过Silverlight插件看到具体占用的空间大小。
IsolatedStorageFile; // 返回一个包括路径和文件的虚拟区域, 用于管理目录,文件等
IsolatedStorageFileStream;//以流的方式读写指定路径的文件.
Published Sunday,March 08,2009 10:05 PM by AustinLG
假设我们并不希望每次都向服务器Request当前用户的信息,那么我们就可以使用IsolateStorage来把我们的用户对象Userinfo保存数据。
我们首先把我们的Object弄出来:
namespace LayoutTemplate2.Entities
{
public class User
{
public string UserID { get; set; }
public string Password { get; set; }
}
}
Published Sunday,March 08,2009 10:12 PM by AustinLG
打开Blend,拖几个个label(就是TextBlock)和几个TextBox,再加几个个按钮,然后回到VS2008,
public partial class Page : UserControl
{
private IsolatedStorageSettings appSetting = IsolatedStorageSettings.ApplicationSettings;
private User user = new User();
private const string KEY_USER = "User";
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
txtUserID.Text = String.Empty;
txtPassword.Text = String.Empty;
Read();
}
// read data from isolate storage
private void Read()
{
if (appSetting.Contains(KEY_USER) == true)
{
var user = (User)appSetting[KEY_USER];
txtUserID.Text = user.UserID;
txtPassword.Text = user.Password;
}
}
private void btnSaveData_Click(object sender, RoutedEventArgs e)
{
if (appSetting.Contains(KEY_USER) == false)
{
appSetting.Add(KEY_USER, null);
}
// user
user.UserID = txtUserID.Text;
user.Password = txtPassword.Text;
// save data into appsetings
appSetting[KEY_USER] = user;
}
private void btnClearData_Click(object sender, RoutedEventArgs e)
{
appSetting.Remove(KEY_USER);
}
private void btnReadData_Click(object sender, RoutedEventArgs e)
{
Read();
}
}
Published Sunday,March 08,2009 10:53 PM by AustinLG
以上为运行效果,接下来使用File存储。
首先修改object
using System.Runtime.Serialization;
[DataContract]
public class User
{
[DataMember]
public string UserID { get; set; }
[DataMember]
public string Password { get; set; }
}
Published Sunday,March 08,2009 11:45 PM by AustinLG
然后增加以下代码:
private void btnSaveData_file_Click(object sender, RoutedEventArgs e)
{
Save();
using (var fs = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream stream = fs.CreateFile(PATH_STORAGE);
DataContractSerializer serializer = new DataContractSerializer(typeof(User));
serializer.WriteObject(stream,user);
stream.Close();
}
}
private void btnReadData_file_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream stream = store.OpenFile(PATH_STORAGE, FileMode.Open);
DataContractSerializer serializer = new DataContractSerializer(typeof(User));
user = (User)serializer.ReadObject(stream);
stream.Close();
txtUserID.Text = user.UserID;
txtPassword.Text = user.Password;
}
}
Published Sunday,March 08,2009 11:45 PM by AustinLG
需要注意,不能使用BinaryFormmatter,这个在Silverlight中是无效的。所以序列化对象,你应该使用DataContractSerializer,而且用起来更简单。