Windows Phone開發(29):隔離存儲C
本文是隔離存儲的第三節,大家先喝杯咖啡放松,今天的內容也是非常簡單,我們就聊一件東東——用戶設置。
當然了,可能翻譯為應用程序設置合適一些,不過沒關系,只要大家明白,它就是用于保存我們的應用程序的設置信息就行了。
它屬于字典集合,每一項保存的數據都以鍵-值對的形式存儲,鍵值是字符串類型,不能為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這個不用說了,你懂的。
事不宜遲,做個練習,你馬上就會懂的。
這個例子很簡單了,在兩個文本框中輸入值,當離開頁面時保存,當用戶導航到頁面后自動加載保存到隔離存儲的數據。
- <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 是包含所有頁面內容的根網格-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel 包含應用程序的名稱和頁標題-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="我的應用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="應用程序設置" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - 在此處放置其他內容-->
- <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="職業:" 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;
- // 構造函數
- 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);
- // 當導航到頁面,讀入數據。
- if (MySetting.Contains("name"))
- {
- txtName.Text = MySetting["name"] as string;
- }
- if (MySetting.Contains("pos"))
- {
- txtPos.Text = MySetting["pos"] as string;
- }
- }
- }
- }
好的,看,是不是很簡單?
隔離存儲就吹到這兒了,從下一篇文章開始,我們不玩抽象,我們來一些“所見即得”的東東,一起當一回山寨畫家,從下一節開始,我們一起討論圖形、畫刷、繪圖相關的內容。