Windows Phone笔记(9)使用独立存储(上)(转)

Windows Phone笔记(9)使用独立存储(上)

 

  在前面的笔记中我们了解到如何在页面间共享数据,但是这些数据是保持在内存中的,当程序被终止时存储的数据就会丢失,在很多时候我们有必要对数据进行持久化,例如,存储程序的配置,启动信息等。在Windows Phone中我们可以通过独立存储来实现数据的持久化。但是在这里为了提高系统的安全性,Windows Phone中的应用程序中所有的I/O操作只限于使用独立存储,并且只能访问本应用程序目录下的独立存储

  首先根据要存储的数据类型,Windows Phone的独立存储的使用类型有下面三种,我们会对下面的使用独立存储的类型分别进行介绍,以及如何使用它。

  • 设置:使用 IsolatedStorageSettings 类将数据存储为键/值对。

  • 文件和文件夹:使用 IsolatedStorageFile 类存储文件和文件夹。

  • 关系数据:使用 LINQ to SQL 将关系数据存储在本地数据库中。    

使用IsolatedStorageSettings类保存应用程序设置

  在我们的应用程序中,经常会对应用程序做一些设置,以符合应用程序的需要,或适应自己的操作习惯。比如说对应用程序更换主题、皮肤,对应用程序的启动设置等等。要想实现这些功能我们必须对我们队应用程序作出的更改和设置数据进行持久化,即使程序关闭了,下次也一样可以范围这些存储的数据,在Windows Phone中我们使用IsolatedStorageSettings类对应用程序进行设置,该类使用键值对的方式来存储数据。下面我们通过一个简单示例来演示如何使用IsolatedStorageSetting类来存储数据。

  该示例的作用是保存页面退出前的背景色,重写运行程序后加载还原保存的背景色。只有一个MainPage页面,前台页面的代码不需要修改,这里就这在给出。

首先我们还是需要使用在上一篇笔记中使用过的App类,因为该类的实例存在于整个应用程序的生命周期中的,所有很适合用来在整个应用程序中共享数据,着这里我们在App类中添加一个属性,用来保存背景色,以及添加两个方法,分别用来保存和加载程序的设置,需要注意的使我们的App类是一个分部类(可以将类、结构、接口的定义拆分到两个或多个源文件):

复制代码
 1     public partial class App : Application
2 {
3 //应用程序设置
4 public Brush MainPageBackgroundBrush { get; set; }
5
6 /// <summary>
7     /// 加载设置
8      /// </summary>
9 void LoadSettings()
10 {
11 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
12 Color mainPageClr;
13 //检查是否保存了背景色
14 if (settings.TryGetValue<Color>("MainPageBackgroundColor", out mainPageClr))
15 {
16 this.MainPageBackgroundBrush = new SolidColorBrush(mainPageClr);//加载保存的设置
17 }
18 }
19
20 /// <summary>
21      /// 保存设置
22      /// </summary>
23 void SaveSettings()
24 {
25 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
26
27 if (this.MainPageBackgroundBrush is SolidColorBrush)
28 {
29 settings["MainPageBackgroundColor"] = (this.MainPageBackgroundBrush as SolidColorBrush).Color;//保存设置
30 }
31 settings.Save();
32
33 }
34 }
复制代码

那么现在就有一个问题了,我们该什么时候保存设置?什么时候又该加载设置呢?我们可以通过利用Windows Phone中应用程序页面的生命周期的特点来编写相关的代码。在Windows Phone中我们可以利用应用程序状态管理的4个主事件(包含在Application对象中)来完成我们的操作,它们分别是:

  • Launching:当应用程序启动时发生。

  • Deactivated:当应用程序被取消激活时发生。

  • Activated:当之前处于休眠状态或被逻辑删除之后应用程序变为活动时发生。

  • Closing:当应用程序退出时发生。

Windows Phone应用程序的保留状态所必须处理的生命周期、执行流和事件。

        十秒原则:如果保存临时状态所花的时间超过 10 秒,则系统将终止应用程序。

了解了这四个程序状态管理的事件后,前面的问题我们就很容易的回答了:Launching事件和Activated事件发生时,我们加载设置Deactivated事件和Closing事件发生时,我们保持设置。下面我们把保存和加载设置方法的调用放在,Windows Phone项目生成的模板中已经添加的这些事件状态管理函数中,下面是App.xaml.cs添加的代码:

复制代码
 1      // 应用程序启动(例如,从“开始”菜单启动)时执行的代码
2      // 此代码在重新激活应用程序时不执行
3 private void Application_Launching(object sender, LaunchingEventArgs e)
4 {
5 LoadSettings();//加载设置
6 }
7
8 // 激活应用程序(置于前台)时执行的代码
9     // 此代码在首次启动应用程序时不执行
10 private void Application_Activated(object sender, ActivatedEventArgs e)
11 {
12 LoadSettings();//加载设置
13 }
14
15 // 停用应用程序(发送到后台)时执行的代码
16     // 此代码在应用程序关闭时不执行
17 private void Application_Deactivated(object sender, DeactivatedEventArgs e)
18 {
19 SaveSettings();//保存设置
20 }
21
22 // 应用程序关闭(例如,用户点击“后退”)时执行的代码
23     // 此代码在停用应用程序时不执行
24 private void Application_Closing(object sender, ClosingEventArgs e)
25 {
26 SaveSettings();//保存设置
27 }
复制代码

 

准备工作都已经做好了,那么我们下面给出我们MainPage.xaml.cs的后台处理代码:

复制代码
 1 public partial class MainPage : PhoneApplicationPage
2 {
3 Random ran = new Random();
4 // 构造函数
5 public MainPage()
6 {
7 InitializeComponent();
8 //为使用独立存储设置访问App类
9 Brush brush = (Application.Current as App).MainPageBackgroundBrush;
10
11 if (brush != null)
12 {
13 this.ContentPanel.Background = brush;
14 }
15 }
16
17 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
18 {
19 SolidColorBrush brush = new SolidColorBrush(Color.FromArgb
20 (255, (byte)ran.Next(255), (byte)ran.Next(255), (byte)ran.Next(255)));
21 this.ContentPanel.Background = brush;
22
23 //为使用独立存储设置,保存到App类
24 (Application.Current as App).MainPageBackgroundBrush = brush;
25
26 base.OnManipulationStarted(e);
27 }
28 }
复制代码

编译运行程序:

运行程序随机生成一个背景色,然后点击手机的"Back","Start"按钮,重新回到我们的应用程序,我们可以看到背景色已经被加载为上次程序退出时保持的设置。

好,那么这里我们已经知道了如何通过使用IsolatedStorageSettings 类来保持应用程序的设置,在后面的笔记中我们学习如何使用 IsolatedStorageFile 类存储文件和文件夹。

 

参考资料:

  http://msdn.microsoft.com/zh-cn/library/ff817008(v=vs.92).aspx(重要)

  http://msdn.microsoft.com/zh-cn/library/ff967547(v=vs.92).aspx

  http://msdn.microsoft.com/zh-cn/library/hh307995.aspx(重要)

  http://msdn.microsoft.com/zh-cn/library/ff967548(v=vs.92).aspx

  《Programming Windows Phone 7 Microsoft Silverlight Edition》

作者:晴天猪

出处:http://www.cnblogs.com/IPrograming

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted on 2012-05-03 17:18  voker  阅读(167)  评论(0编辑  收藏  举报

导航