Windows Phone文件/SD卡操作 (2)


1.3 IsolatedStorageSettings
Isolated Storage不但提供了文件相关操作支持,还提供了IsolatedStorageSettings类,用来保存应用程序状态信息,我们在第三章中介绍工程结构的时候曾经涉及到应用程序的生命周期,打开任何一个工程的App.xaml文件,你会看到以下代码
  1. <Application.ApplicationLifetimeObjects>
  2. <!--Required object that handles lifetime events for the application-->
  3. <shell:PhoneApplicationService
  4. Launching="Application_Launching" Closing="Application_Closing"
  5. Activated="Application_Activated" Deactivated="Application_Deactivated"/>
  6. </Application.ApplicationLifetimeObjects>
复制代码
其中PhoneApplicationService可以用来保存暂时状态,程序一旦退出状态信息就会丢失,它还是我们设置程序状态变换时间处理的地方。
应用程序启动时候会产生Lauching事件,我们会通过Application_Launching处理函数得到通知;当用户按下Windows键后,用户会从当前应用程序离开,这个过程叫TombStone,这时候产生Deactivated事件;如果用户这时候再按一下返回键(左箭头按键),这时候会产生Activated事件,该应用程序开始恢复;如果用户启动引用程序后直接按返回键,这时候该应用程序就会退出,会产生Closing事件;所有这些事件如同Launching事件一样,程序可以选择性处理。
一般应用程序退出或者发生状态变换的时候我们希望保存一下数据,比如用户正在玩一个游戏,当他返回程序的时候,他希望能接着玩,而不是重新开始,这时候就需要程序根据程序状态不同提供保存和恢复功能,而IsolatedStorageSettings则可以达到这一目的。

首先,我们为App.xaml.cs添加两个成员函数
  1. void LoadSettings()
  2. {
  3. IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
  4. String value;
  5. settings.TryGetValue<String>("setting1", out value);
  6. }

  7. void SaveSettings()
  8. {
  9. IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
  10. settings["setting1"] = "value1";
  11. settings.Save();
  12. }
复制代码
在Application_Launching、Application_Activated、Application_Deactivated、Application_Closing添加上LoadSettings或者SaveSettings
  1. // Code to execute when the application is launching (eg, from Start)
  2. // This code will not execute when the application is reactivated
  3. private void Application_Launching(object sender, LaunchingEventArgs e)
  4. {
  5. LoadSettings();
  6. }

  7. // Code to execute when the application is activated (brought to foreground)
  8. // This code will not execute when the application is first launched
  9. private void Application_Activated(object sender, ActivatedEventArgs e)
  10. {
  11. LoadSettings();
  12. }

  13. // Code to execute when the application is deactivated (sent to background)
  14. // This code will not execute when the application is closing
  15. private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  16. {
  17. SaveSettings();
  18. }

  19. // Code to execute when the application is closing (eg, user hit Back)
  20. // This code will not execute when the application is deactivated
  21. private void Application_Closing(object sender, ClosingEventArgs e)
  22. {
  23. SaveSettings();
  24. }
复制代码
我们再次打开Windows Phone Device Manager,看看IsolatedStorageSettings是怎么实现的

PIC sd卡3.jpg
2012-3-2 10:38:55 上传
下载附件 (44.04 KB)


从图中我们可以看到只是在IsolatedStore目录下创建了一个_ApplicationSettings文件而已,那这个文件什么样呢,我们把文件Copy到PC上看一看,它的内容如下:
  1. <ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  2. <KeyValueOfstringanyType>
  3. <Key>setting1</Key>
  4. <Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:string">
  5. value1
  6. </Value>
  7. </KeyValueOfstringanyType>
  8. </ArrayOfKeyValueOfstringanyType>
复制代码
有Android或者iOS经验的开发者可能多少有点似曾相识的感觉,因为Android中的Shared Preference还有iOS的plist都可以用来保存设置信息,而且都是xml结构的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值