Windows Phone 7的本地存储——IsolatedStorageSettings
来源:互联网 发布:网络机柜跳线是什么 编辑:程序博客网 时间:2024/05/17 03:38
在SDK 7.1中,本地存储大致上共有如下三种方式:
- Settings:通过类IsolatedStorageSettings使用键值对的方式存储信息。
- Files: 通过类IsolatedStorageFile可以实现文件,文件夹的管理。
- Local Database: 使用LINQ to SQL来实现对local database的存储及访问。
今天我们来了解如何通过IsolatedStorageSettings来实现信息的本地存储。
在WP中,使用IsolatedStorageSettings来存储本地信息,主要是通过IsolatedStorageSettings中的ApplicationSettings,比如通过下面的代码添加一个新的键值对
1 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;2 settings.Add(key, value);
或者根据Key获取所对应的Value
1 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;2 if (settings.Contains(key))3 {4 string value = settings[key].ToString();5 }
通过上述的方法对setting的值进行操作,我们会发现需要去检查key是否存在,并且对应的Value值是已Object的方式存储的,需要转换成相应的数据类型,如果我们的app中有很多的setting需要处理,这些重复的过程就比较繁琐,所以我们可以封装一个类用来处理对这些setting键值对的处理。
1 using System.IO.IsolatedStorage; 2 3 namespace FlorCava.WPBasic.StorageSetting 4 { 5 public class Setting<T> 6 { 7 string name; 8 T value; 9 T defaultValue;10 bool hasValue;11 12 public Setting(string name, T defaultValue)13 {14 this.name = name;15 this.defaultValue = defaultValue;16 }17 18 public T Value19 {20 get21 {22 if (!this.hasValue)23 {24 if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name, out this.value))25 {26 this.value = this.defaultValue;27 IsolatedStorageSettings.ApplicationSettings[this.name] = this.value;28 }29 this.hasValue = true;30 }31 32 return this.value;33 }34 set35 {36 IsolatedStorageSettings.ApplicationSettings[this.name] = value;37 this.value = value;38 this.hasValue = true;39 }40 }41 42 public T DefaultValue43 {44 get { return this.defaultValue; }45 }46 }47 }
接下来我们可以创建一个简单的Project来体会对于Setting的处理,创建一个类并利用我们上面封装的Setting类提供两个属性。
1 namespace FlorCava.WPBasic.StorageSetting 2 { 3 public class AppSettings 4 { 5 private Setting<bool> boolSetting = new Setting<bool>("BoolSetting", false); 6 /// <summary> 7 /// test setting for bool 8 /// </summary> 9 public bool BoolSetting10 {11 get12 {13 return boolSetting.Value;14 }15 set16 {17 boolSetting.Value = value;18 }19 }20 21 private Setting<string> stringSetting = new Setting<string>("StringSetting", string.Empty);22 /// <summary>23 /// test setting for string24 /// </summary>25 public string StringSetting26 {27 get28 {29 return stringSetting.Value;30 }31 set32 {33 stringSetting.Value = value;34 }35 }36 37 }38 }
然后在main页面中,添加两个控件CheckBox和TextBox,绑定到相关属性并通过TwoWay的方式来更新对Setting的修改。首先在页面上添加对local的声明:
xmlns:local="clr-namespace:FlorCava.WPBasic.StorageSetting"
并在Source中添加对AppSettings的引用:
<phone:PhoneApplicationPage.Resources> <local:AppSettings x:Key="appSettings"></local:AppSettings> </phone:PhoneApplicationPage.Resources>
最后我们添加两个控件完成对上述AppSetting的绑定,设定绑定模式为TwoWay,这样当我们修改控件的属性或者内容时,就会同步修改在AppSetting中存储的Setting值。
1 <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">2 <TextBlock Text="Bool Setting"/>3 <CheckBox IsChecked="{Binding Source={StaticResource appSettings},Path=BoolSetting, Mode=TwoWay}" />4 <TextBlock Text="String Setting"/>5 <TextBox Text="{Binding Source={StaticResource appSettings}, Path=StringSetting, Mode=TwoWay}"/>6 </StackPanel>
至此已经完成本次对于IsolatedStorageSettings存储测试的编码工作,编译并运行,输入一些内容后,点击Start退出应用,此时会把我们输入的内容通过键值对的方式保存在ApplicationSettings中。
- Windows Phone 7的本地存储——IsolatedStorageSettings
- Windows Phone的本地存储——IsolatedStorageSettings
- Windows Phone 独立存储之IsolatedStorageSettings
- Windows Phone的本地存储——IsolatedStorageFile
- Windows phone 7中,把页面数据存为长期存储(非IsolatedStorageFile),而是用IsolatedStorageSettings
- Windows phone开发IsolatedStorageSettings 操作类
- 08,Windows Phone 本地存储
- Windows Phone 7独立存储之本地数据库
- Windows Phone——本地数据库
- Windows Phone 7 开发——独立存储
- Windows Phone 7独立存储
- 在windows phone 7上获取本地IP的方法
- 浅谈Windows Phone 7本地数据库的选择
- 浅谈Windows Phone 7本地数据库的选择
- 浅谈Windows Phone 7本地数据库的选择
- Windows Phone Mango编程实践——本地数据库
- IsolatedStorageSettings存储数据
- Windows Phone 7 使用本地数据库
- 大学计算机基础论文的具体内容
- java类的内存分配机制
- php讀取excel
- C# 线程 的研究
- oracle用Authid Current_User 定义的建表过程plsql可执行别的用户对象,但是job却不能调用
- Windows Phone 7的本地存储——IsolatedStorageSettings
- 成功移植linux2.6.38内核到TQ2440(转)
- Calendar 使用注意
- 成功移植linux2.6.38内核到TQ2440(转)
- plsql可执行别的用户对象,过程却不行(AUTHID CURRENT_USER)
- Eclipse3.6.2 简单安装SVN等插件
- 队列的顺序存储结构
- JQuery调用webService
- SQL 树形菜单查询,查询本身和所有下级项