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

Windows Phone 7 开发 31 日谈——第15日:独立存储

[复制链接]

尚未签到

发表于 2015-5-8 10:25:34 | 显示全部楼层 |阅读模式
  本文是“Windows Phone 7 开发 31 日谈”系列的第15日。
  昨天,我们讨论了程序中的墓碑机制从而让程序看起来是可以在后台运行的。今天,我们来谈谈在电话中存储本地数据的一种非常棒的方法。使用独立存储。
  什么是独立存储?
  独立存储不是一个新概念。在Silverlight 2中已经在使用了。本质上说这是一种在本地文件系统中存储数据或文件的方式。“独立(isolated)”是因为只有你的程序才可以访问这些数据。如果你有两个应用程序,同时你想在它们之间共享数据的话,最好使用一些类似基于云的可以让你共享数据的服务。一个应用程序不能共享,调用设备上其他的应用程序或与之进行交互。
  设置和文件
  有两种方式在本地存储你的数据。第一是通过库中的键/值对,叫做IsolatedStorageSettings。第二是通过创建真实的文件和目录,叫做IsolatedStorageFile。下图简要介绍了这些(由MSDN提供),我会为每种方式提供一个深入的例子。
DSC0000.jpg
  IsolatedStorageSettings
  有很多时候,这可能是你需要的唯一存储方式。IsolatedStorageSettings允许你在一个字典中存储键/值对(注意,无需任何设定),然后再读取出来。这些数据会一直保存着,无论应用程序停止/启动,或者关机等等。除非你删除它,或者用户卸载你的应用程序,否则它一直存在。要记住的一点是在它被添加到字典中之前你无法读取它。在我的每个例子中,你都会看到在读取数据之前检查值是否它存在的代码。下面的例子是在用户在你的程序中接收电子邮件更新时需要保存用户设定的代码。我用了一个多选框允许用户选择,还有一个将此值保存到独立存储中的事件。

DSC0001.gif DSC0002.gif 代码

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;
        }
    }
}正如你所见,这非常简单。请记住以下内容:  
       
  • 如果还没在IsolatedStorageSettings中创建就读取它的值会抛出一个异常。确认你已经初始化了设置,或者总是使用Contains方法先检查一下。   
  • 你可以在设置中保存任意内容。在我的例子中,我保存了一个布尔值,但你可以保存一个客户对象,或者任何你能想到的。   
  • 记住当你读取数据时你需要将它显示强制转换。你会看到我在使用之前将数据转换为bool值。虽然你保存了对象,但并没有保存它的类型。是否能看到类型取决于你自己。   
  • 设置一个值和在库中添加它效果是一样。“settings.Add()”的语句实际上不是必需的,我添加它是为了让你看清语法。
    就这些。IsolatedStorageSettings非常简单。只用极少的代码就可保存键/值对。创建和保存文件相对略复杂一些,但还是十分简单。
  IsolatedStorageFile
  使用IsolatedStorageFile是一种让你可以在用户的设备中存储真实文件的机制。在我的例子中,在一个子目录中创建了一个文本文件,并读取文件中的内容。我们还可以创建和删除目录,子目录及文件。看起来有很多代码,但实际上非常简单。我们创建一个新的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.";
    }
}离开程序时这多像一个迷人的魔术,再回来时,会再次载入文件(它还在那儿!)。    你都知道了。现在我们在Windows Phone 7中有两种存储机制可以用。IsolatedStorageSettings和IsolatedStorageFile。我很乐意听到你在程序中使用这两种存储结构的创新用法。请留言!
  下载代码示例
  这个例子将上面展示的代码融合到了一个项目中。
DSC0003.jpg
  原文地址:http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-15-Isolated-Storage.aspx

运维网声明 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-64907-1-1.html 上篇帖子: Windows Phone 7 开发 31 日谈——第17日:枢轴控件 下篇帖子: Windows Phone 7 开发 31 日谈——第12日:使手机震动
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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