设为首页 收藏本站
查看: 769|回复: 0

windows phone 7 31 天学习笔记 15: 独立存储

[复制链接]

尚未签到

发表于 2015-5-13 06:50:31 | 显示全部楼层 |阅读模式
  原文地址:http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-15-Isolated-Storage.aspx
  本文讨论Windows phone 7内部的数据存储方式。
  什么是独立存储?
  独立存储不是一个新鲜的事物。他在Silverlight2中已经被使用了。他是在本地文件系统中保存数据文件的一种基本方式,说他独立是因为只有你的程序能够接触到这些数据,如果你有两个程序,并且想在相互之间共享数据,那你最好把数据放到云端。程序没有方法在本地进行数据交互。
  设置以及文件
  有两种方式来本地保存数据。一种方式是通过Key/Value保存,我们称其为独立存储设置。第二种方式是实际保存文件或是创建文件夹。这叫做独立存储文件。下图是对两种方式方式的简单阐述:
DSC0000.png
  独立存储设置
  出于很多原因,这也许是你唯一的选择。独立存储设置运行你以字典的方式保存有用的数据。他将会在程序启动或是没电的情况下以及存在,除非你卸载了程序。还有一点就是在添加到库之前不能够检索值。在我的示例中,你会看到我在检索值之前首先是看其是否存在。下面是保存用户的邮件偏好的示例程序。我有一个选择框,允许用户设置:
  
   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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-66356-1-1.html 上篇帖子: [转]Windows Phone 7开发者注册Marketplace 下篇帖子: Windows 7 里进程管理器里面的各列是什么含义?主要是和内存有关的内存-专用工作集,内存-工作集,内存-提交大小???
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表