Windows Phone 7 Belling‘s课堂(七) 独立存储空间(3)

隔离存储的第三节,大家先喝杯咖啡放松,今天的内容也是非常简单,我们就聊一件东东——用户设置。
当然了,可能翻译为应用程序设置合适一些,不过没关系,只要大家明白,它就是用于保存我们的应用程序的设置信息就行了。

它属于字典集合,每一项保存的数据都以键-值对的形式存储,键值是字符串类型,不能为null,注意啊,不然会引发异常,当然,估计也没有人这么无聊,把空值保存。

使用方法很简单,通过IsolatedStorageSettings的ApplicationSettings静态属必返回一个IsolatedStorageSettings实例,然后呢,你就可随便耍了。

1、要向集合加入数据可调用Add方法,它的定义如下:
     public void Add(string key, object value)
相信大家知道怎么用了。

2、使用Contains(string key)方法检测一下某个键是否存在,在读取上次保存的数据时,这是必须的。

3、Clear方法不用我介绍,一看名字就知道非常恐怖,一不小心,就把你写入的所有设置都清除,当然,在没有调用Save方法前,还没有写入隔离存储区的。

4、Remove(string key)方法,当你觉得某项设置不需要了,或者你觉得它人品太差,需要删除,可以调用该方法,参数更不用我说了,对就是要删除的项的键,比如你是QQ群主,你想踢掉某个人品值较低的成员时,你肯定要先知道TA的QQ号或昵称。

5、Save这个不用说了,你懂的。

事不宜迟,做个练习,你马上就会懂的。
这个例子很简单了,在两个文本框中输入值,当离开页面时保存,当用户导航到页面后自动加载保存到隔离存储的数据。

 

前台UI 代码:

View Code
 1 <phone:PhoneApplicationPage   
 2     x:Class="PhoneApp1.MainPage"  
 3    
 4     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
 5     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
 6     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
 7     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
 8     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
 9     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
10     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"  
11     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
12     FontSize="{StaticResource PhoneFontSizeNormal}"  
13     Foreground="{StaticResource PhoneForegroundBrush}"  
14     SupportedOrientations="Portrait" Orientation="Portrait"  
15     shell:SystemTray.IsVisible="True">
16 
17     <!--LayoutRoot 是包含所有页面内容的根网格-->
18     <Grid x:Name="LayoutRoot" Background="Transparent">
19         <Grid.RowDefinitions>
20             <RowDefinition Height="Auto"/>
21             <RowDefinition Height="*"/>
22         </Grid.RowDefinitions>
23 
24         <!--TitlePanel 包含应用程序的名称和页标题-->
25         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
26             <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
27             <TextBlock x:Name="PageTitle" Text="应用程序设置" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
28         </StackPanel>
29 
30         <!--ContentPanel - 在此处放置其他内容-->
31         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
32             <TextBlock Height="30" HorizontalAlignment="Left" Margin="33,50,0,0" Name="textBlock1" Text="姓名:" VerticalAlignment="Top" />
33             <TextBlock Height="30" HorizontalAlignment="Left" Margin="33,116,0,0" Name="textBlock2" Text="职业:" VerticalAlignment="Top" />
34             <TextBox Height="72" HorizontalAlignment="Left" Margin="116,25,0,0" Name="txtName" Text="" VerticalAlignment="Top" Width="292" />
35             <TextBox Height="72" HorizontalAlignment="Left" Margin="116,104,0,0" Name="txtPos" Text="" VerticalAlignment="Top" Width="292" />
36         </Grid>
37     </Grid>
38 
39 </phone:PhoneApplicationPage>  

后台CS代码:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13 
14 using System.IO.IsolatedStorage;
15 
16 namespace PhoneApp1
17 {
18     public partial class MainPage : PhoneApplicationPage
19     {
20         // 声明一个IsolatedStorageSettings变量。  
21         IsolatedStorageSettings MySetting = IsolatedStorageSettings.ApplicationSettings;
22         // 构造函数  
23         public MainPage()
24         {
25             InitializeComponent();
26         }
27 
28         protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
29         {
30             base.OnNavigatedFrom(e);
31             MySetting["name"] = this.txtName.Text;
32             MySetting["pos"] = txtPos.Text;
33             // 保存  
34             MySetting.Save();
35         }
36 
37         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
38         {
39             base.OnNavigatedTo(e);
40             // 当导航到页面,读入数据。  
41             if (MySetting.Contains("name"))
42             {
43                 txtName.Text = MySetting["name"] as string;
44             }
45             if (MySetting.Contains("pos"))
46             {
47                 txtPos.Text = MySetting["pos"] as string;
48             }
49         }
50     }
51 }

posted @ 2012-12-10 10:16  BellingWP  阅读(159)  评论(0编辑  收藏  举报