Silverlight Cache类的简单实现方式
新建立一个静态的Cache类 Cache大家应该都清楚 是运用键(key)-值(value)来实现的 本文的方式主要是运用silverlight 存储区中放置一个缓存文件来实现的 非常简单的一种方式 比网络上其他的Cache要简单很多。写入的是XML文件 本文转自:http://www.ywrj.net/a/NET/SL_WPF/20120724/11478.html 转载请注明出处
public static class Cache
public static class Cache
{
// Methods 取值
public static string Get(string key)
{
string str = string.Empty;
try
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream stream = file.OpenFile(key, FileMode.OpenOrCreate);
if (stream.Length > 0L)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(string));
str = serializer.ReadObject(stream).ToString();
}
stream.Close();
}
}
catch { }
return str;
}
//赋值
public static void Set(string key, string value)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream stream = file.CreateFile(key);//(www.ywrj.net)创建一个以key值命名的存储文件
new DataContractSerializer(typeof(string)).WriteObject(stream, value);//当然是往文件中写数据了哦
stream.Close();
}
}
}
至于用法就不用我多累赘了吧。注意取值时用的key和赋值时的key要一致哦
浙公网安备 33010602011771号