Windows Phone 7 不温不火学习之《独立存储空间》
2010-12-29 22:42 Terry_龙 阅读(2152) 评论(7) 编辑 收藏 举报在Android 里面我们要快速保存用户的设置或者游戏进行的数据,我们通常全使用SharePreference 这个类来进行操作,另外Android 还提供了一系列继承自SharePreference 的组件提供我们快速保存用户的设置项。那么在Windows Phone 7 提供了什么样的机制提供用户快速保存数据呢?微软使用了一个叫IsolatedStorageSettings 的类库提供给开发人员快速的使用独立存储保存用户数据的功能,但总体使用感觉来说没有Android 使用的方便,另外Andriod 的类似这种数据存储是暴露给用户的,而Windows Phone 7 的这种存储机制则是严格控制,我们开发人员也不知道其具体存放位置。本篇的学习笔记,我利用Android 的存储特色使用Windows Phone 7 的 IsolatedStorageSettings 类模仿了一个类似 Android 存储机制的DEMO,希望这个小DEMO能对你有所帮助,下面先给出效果图:
首次进入页面时,界面放置了一个 TextBlock 控件和一个导航的按钮,点击导航按钮进入配置界面,如下图:
如上图,图中有一系列组件,可以设置第一幅图的TextBlock 的字体大小和字体需要显示的颜色,选择红色并且改变字体大小为32,然后点击Back 键退回来,显示效果如下:
虽说这是一个非常简单的DEMO,但Windows Phone 7的IsolatedStorageSettings 在这个DEMO中的使用是比较全面的,下面给出代码示意:
首先,mainPage 界面的CS代码
{
// Constructor
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains(App.textColor))
{
int colorInt = Int32.Parse(IsolatedStorageSettings.ApplicationSettings[App.textColor].ToString());
SolidColorBrush scb=null;
switch (colorInt)
{
case 0:
scb = new SolidColorBrush(Colors.White);
break;
case 1:
scb = new SolidColorBrush(Colors.Red);
break;
case 2:
scb = new SolidColorBrush(Colors.Green);
break;
case 3:
scb = new SolidColorBrush(Colors.Blue);
break;
}
setTextBlock.Foreground = scb;
}
if (IsolatedStorageSettings.ApplicationSettings.Contains(App.textSize))
{
int textSize = Int32.Parse(IsolatedStorageSettings.ApplicationSettings[App.textSize].ToString());
setTextBlock.FontSize = textSize;
}
}
private void navigateionBtn_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Set.xaml",UriKind.RelativeOrAbsolute));
}
当界面加载时,读取配置信息并修改TextBlock 对应的值。进入配置页面时稍显复杂,考虑到不需要用户点确认就可以保存数据,这里使用的导航进来和导航出去两种写法,代码如下:
{
int textColor;
int textSize;
public Set()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Set_Loaded);
}
void Set_Loaded(object sender, RoutedEventArgs e)
{
switch (textColor)
{
case 0:
whiteRadioButton.IsChecked = true;
break;
case 1:
redRadioButton.IsChecked = true;
break;
case 2:
greenRadioButton.IsChecked = true;
break;
case 3:
blueRadioButton.IsChecked = true;
break;
}
textSizeTextBox.Text = textSize.ToString();
}
private int colorInt = -1;
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
if (whiteRadioButton.IsChecked==true)
{
colorInt = 0;
}
else if (redRadioButton.IsChecked==true)
{
colorInt = 1;
}
else if (greenRadioButton.IsChecked==true)
{
colorInt = 2;
}
else if (blueRadioButton.IsChecked==true)
{
colorInt = 3;
}
IsolatedStorageSettings.ApplicationSettings[App.textColor] = colorInt;
IsolatedStorageSettings.ApplicationSettings[App.textSize] = Int32.Parse(textSizeTextBox.Text);
IsolatedStorageSettings.ApplicationSettings.Save();
base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains(App.textColor))
{
textColor = Int32.Parse(IsolatedStorageSettings.ApplicationSettings[App.textColor].ToString());
}
if (IsolatedStorageSettings.ApplicationSettings.Contains(App.textSize))
{
textSize = Int32.Parse(IsolatedStorageSettings.ApplicationSettings[App.textSize].ToString());
}
base.OnNavigatedTo(e);
}
}
导航进来时,为变量赋值,并在加载完成后改变控件的内容和状态,如上图的TextBox 的文本值和RadioButton的选中项。
导航出去时,保存用户的选择行为。
Tip:IsolatedStorageSettings.ApplicationSettings 这个 Ditonary 的Value 是一个 Object ,亦就是您可以将你的一个Model 对象保存进来亦可,它内部会帮你序列化。
另外,如果你习惯使用IO流写文件操作,下篇文章会讲述到使用IO流写文件的方式存储空间的方法,希望 留意。
源码下载:存储空间DEMO
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架