`
izuoyan
  • 浏览: 8915025 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Windows Phone 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代码

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicpartialclassMainPage:PhoneApplicationPage
{
//Constructor
publicMainPage()
{
InitializeComponent();
this.Loaded+=newRoutedEventHandler(MainPage_Loaded);
}

voidMainPage_Loaded(objectsender,RoutedEventArgse)
{
if(IsolatedStorageSettings.ApplicationSettings.Contains(App.textColor))
{
intcolorInt=Int32.Parse(IsolatedStorageSettings.ApplicationSettings[App.textColor].ToString());
SolidColorBrushscb
=null;
switch(colorInt)
{
case0:
scb
=newSolidColorBrush(Colors.White);
break;
case1:
scb
=newSolidColorBrush(Colors.Red);
break;
case2:
scb
=newSolidColorBrush(Colors.Green);
break;
case3:
scb
=newSolidColorBrush(Colors.Blue);
break;
}
setTextBlock.Foreground
=scb;
}

if(IsolatedStorageSettings.ApplicationSettings.Contains(App.textSize))
{
inttextSize=Int32.Parse(IsolatedStorageSettings.ApplicationSettings[App.textSize].ToString());
setTextBlock.FontSize
=textSize;
}
}

privatevoidnavigateionBtn_Click(objectsender,RoutedEventArgse)
{
NavigationService.Navigate(
newUri("/Set.xaml",UriKind.RelativeOrAbsolute));
}

当界面加载时,读取配置信息并修改TextBlock 对应的值。进入配置页面时稍显复杂,考虑到不需要用户点确认就可以保存数据,这里使用的导航进来和导航出去两种写法,代码如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicpartialclassSet:PhoneApplicationPage
{

inttextColor;
inttextSize;
publicSet()
{
InitializeComponent();
this.Loaded+=newRoutedEventHandler(Set_Loaded);
}

voidSet_Loaded(objectsender,RoutedEventArgse)
{
switch(textColor)
{
case0:
whiteRadioButton.IsChecked
=true;
break;
case1:
redRadioButton.IsChecked
=true;
break;
case2:
greenRadioButton.IsChecked
=true;
break;
case3:
blueRadioButton.IsChecked
=true;
break;
}
textSizeTextBox.Text
=textSize.ToString();
}


privateintcolorInt=-1;
protectedoverridevoidOnNavigatedFrom(System.Windows.Navigation.NavigationEventArgse)
{
if(whiteRadioButton.IsChecked==true)
{
colorInt
=0;
}
elseif(redRadioButton.IsChecked==true)
{
colorInt
=1;
}
elseif(greenRadioButton.IsChecked==true)
{
colorInt
=2;
}
elseif(blueRadioButton.IsChecked==true)
{
colorInt
=3;
}

IsolatedStorageSettings.ApplicationSettings[App.textColor]
=colorInt;

IsolatedStorageSettings.ApplicationSettings[App.textSize]
=Int32.Parse(textSizeTextBox.Text);
IsolatedStorageSettings.ApplicationSettings.Save();

base.OnNavigatedFrom(e);
}

protectedoverridevoidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse)
{
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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics