Windows Phone開發(fā)(29):隔離存儲C
本文是隔離存儲的第三節(jié),大家先喝杯咖啡放松,今天的內(nèi)容也是非常簡單,我們就聊一件東東——用戶設(shè)置。
當(dāng)然了,可能翻譯為應(yīng)用程序設(shè)置合適一些,不過沒關(guān)系,只要大家明白,它就是用于保存我們的應(yīng)用程序的設(shè)置信息就行了。
它屬于字典集合,每一項保存的數(shù)據(jù)都以鍵-值對的形式存儲,鍵值是字符串類型,不能為null,注意啊,不然會引發(fā)異常,當(dāng)然,估計也沒有人這么無聊,把空值保存。
使用方法很簡單,通過IsolatedStorageSettings的ApplicationSettings靜態(tài)屬必返回一個IsolatedStorageSettings實例,然后呢,你就可隨便耍了。
1、要向集合加入數(shù)據(jù)可調(diào)用Add方法,它的定義如下:
public void Add(string key, object value)
相信大家知道怎么用了。
2、使用Contains(string key)方法檢測一下某個鍵是否存在,在讀取上次保存的數(shù)據(jù)時,這是必須的。
3、Clear方法不用我介紹,一看名字就知道非??植?,一不小心,就把你寫入的所有設(shè)置都清除,當(dāng)然,在沒有調(diào)用Save方法前,還沒有寫入隔離存儲區(qū)的。
4、Remove(string key)方法,當(dāng)你覺得某項設(shè)置不需要了,或者你覺得它人品太差,需要刪除,可以調(diào)用該方法,參數(shù)更不用我說了,對就是要刪除的項的鍵,比如你是QQ群主,你想踢掉某個人品值較低的成員時,你肯定要先知道TA的QQ號或昵稱。
5、Save這個不用說了,你懂的。
事不宜遲,做個練習(xí),你馬上就會懂的。
這個例子很簡單了,在兩個文本框中輸入值,當(dāng)離開頁面時保存,當(dāng)用戶導(dǎo)航到頁面后自動加載保存到隔離存儲的數(shù)據(jù)。
- <phone:PhoneApplicationPage
- x:Class="PhoneApp1.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
- xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- SupportedOrientations="Portrait" Orientation="Portrait"
- shell:SystemTray.IsVisible="True">
- <!--LayoutRoot 是包含所有頁面內(nèi)容的根網(wǎng)格-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel 包含應(yīng)用程序的名稱和頁標(biāo)題-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="我的應(yīng)用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="應(yīng)用程序設(shè)置" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - 在此處放置其他內(nèi)容-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <TextBlock Height="30" HorizontalAlignment="Left" Margin="33,50,0,0" Name="textBlock1" Text="姓名:" VerticalAlignment="Top" />
- <TextBlock Height="30" HorizontalAlignment="Left" Margin="33,116,0,0" Name="textBlock2" Text="職業(yè):" VerticalAlignment="Top" />
- <TextBox Height="72" HorizontalAlignment="Left" Margin="116,25,0,0" Name="txtName" Text="" VerticalAlignment="Top" Width="292" />
- <TextBox Height="72" HorizontalAlignment="Left" Margin="116,104,0,0" Name="txtPos" Text="" VerticalAlignment="Top" Width="292" />
- </Grid>
- </Grid>
- </phone:PhoneApplicationPage>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using Microsoft.Phone.Controls;
- using System.IO.IsolatedStorage;
- namespace PhoneApp1
- {
- public partial class MainPage : PhoneApplicationPage
- {
- // 聲明一個IsolatedStorageSettings變量。
- IsolatedStorageSettings MySetting = IsolatedStorageSettings.ApplicationSettings;
- // 構(gòu)造函數(shù)
- public MainPage()
- {
- InitializeComponent();
- }
- protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
- {
- base.OnNavigatedFrom(e);
- MySetting["name"] = this.txtName.Text;
- MySetting["pos"] = txtPos.Text;
- // 保存
- MySetting.Save();
- }
- protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
- {
- base.OnNavigatedTo(e);
- // 當(dāng)導(dǎo)航到頁面,讀入數(shù)據(jù)。
- if (MySetting.Contains("name"))
- {
- txtName.Text = MySetting["name"] as string;
- }
- if (MySetting.Contains("pos"))
- {
- txtPos.Text = MySetting["pos"] as string;
- }
- }
- }
- }
好的,看,是不是很簡單?
隔離存儲就吹到這兒了,從下一篇文章開始,我們不玩抽象,我們來一些“所見即得”的東東,一起當(dāng)一回山寨畫家,從下一節(jié)開始,我們一起討論圖形、畫刷、繪圖相關(guān)的內(nèi)容。