• Windows Phone开发(29):隔离存储C 转:http://blog.csdn.net/tcjiaan/article/details/7447469


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

    它属于字典集合,每一项保存的数据都以键-值对的形式存储,键值是字符串类型,不能为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这个不用说了,你懂的。


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

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


     

    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. }  



    好的,看,是不是很简单?

    隔离存储就吹到这儿了,从下一篇文章开始,我们不玩抽象,我们来一些“所见即得”的东东,一起当一回山寨画家,从下一节开始,我们一起讨论图形、画刷、绘图相关的内容。

  • 相关阅读:
    【北邮人论坛帖子备份】【心得】20年公考经验分享
    如何写一封国际会议的交流信?
    花呗广告趣图
    《第九个寡妇》读后感
    沟通的五个层次
    部署多功能模块依赖项目中解决的问题
    maven: can't resolve plugin xxxmaven-xxxx-plugin:x.x
    C++编译报错:need 'typename' before 'std::map<T, S>::iterator' because 'std::map<T, S>' is a dependent scope
    详细js中(function(window,document,undefined))的作用
    201509020-js
  • 原文地址:https://www.cnblogs.com/songtzu/p/2607126.html
Copyright © 2020-2023  润新知