Windows Phone開發(29):隔離儲存C

來源:互聯網
上載者:User

本文是隔離儲存的第三節,大家先喝杯咖啡放鬆,今天的內容也是非常簡單,我們就聊一件東東——使用者佈建。
當然了,可能翻譯為應用程式設定合適一些,不過沒關係,只要大家明白,它就是用於儲存我們的應用程式的設定資訊就行了。

它屬於字典集合,每一項儲存的資料都以鍵-值對的形式儲存,索引值是字串類型,不能為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群主,你想踢掉某個人品值較低的成員時,你肯定要Crowdsourced Security Testing道TA的QQ號或暱稱。

5、Save這個不用說了,你懂的。

事不宜遲,做個練習,你馬上就會懂的。
這個例子很簡單了,在兩個文字框中輸入值,當離開頁面時儲存,當使用者導航到頁面後自動載入儲存到隔離儲存的資料。

<phone:PhoneApplicationPage<br /> x:Class="PhoneApp1.MainPage"<br /> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br /> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br /> xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"<br /> xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"<br /> xmlns:d="http://schemas.microsoft.com/expression/blend/2008"<br /> xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"<br /> mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"<br /> FontFamily="{StaticResource PhoneFontFamilyNormal}"<br /> FontSize="{StaticResource PhoneFontSizeNormal}"<br /> Foreground="{StaticResource PhoneForegroundBrush}"<br /> SupportedOrientations="Portrait" Orientation="Portrait"<br /> shell:SystemTray.IsVisible="True"></p><p> <!--LayoutRoot 是包含所有頁面內容的根網格--><br /> <Grid x:Name="LayoutRoot" Background="Transparent"><br /> <Grid.RowDefinitions><br /> <RowDefinition Height="Auto"/><br /> <RowDefinition Height="*"/><br /> </Grid.RowDefinitions></p><p> <!--TitlePanel 包含應用程式的名稱和網頁標題--><br /> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"><br /> <TextBlock x:Name="ApplicationTitle" Text="我的應用程式" Style="{StaticResource PhoneTextNormalStyle}"/><br /> <TextBlock x:Name="PageTitle" Text="應用程式設定" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/><br /> </StackPanel></p><p> <!--ContentPanel - 在此處放置其他內容--><br /> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"><br /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="33,50,0,0" Name="textBlock1" Text="姓名:" VerticalAlignment="Top" /><br /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="33,116,0,0" Name="textBlock2" Text="職業:" VerticalAlignment="Top" /><br /> <TextBox Height="72" HorizontalAlignment="Left" Margin="116,25,0,0" Name="txtName" Text="" VerticalAlignment="Top" Width="292" /><br /> <TextBox Height="72" HorizontalAlignment="Left" Margin="116,104,0,0" Name="txtPos" Text="" VerticalAlignment="Top" Width="292" /><br /> </Grid><br /> </Grid></p><p></phone:PhoneApplicationPage>

 

using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Net;<br />using System.Windows;<br />using System.Windows.Controls;<br />using System.Windows.Documents;<br />using System.Windows.Input;<br />using System.Windows.Media;<br />using System.Windows.Media.Animation;<br />using System.Windows.Shapes;<br />using Microsoft.Phone.Controls;</p><p>using System.IO.IsolatedStorage;</p><p>namespace PhoneApp1<br />{<br /> public partial class MainPage : PhoneApplicationPage<br /> {<br /> // 聲明一個IsolatedStorageSettings變數。<br /> IsolatedStorageSettings MySetting = IsolatedStorageSettings.ApplicationSettings;<br /> // 建構函式<br /> public MainPage()<br /> {<br /> InitializeComponent();<br /> }</p><p> protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)<br /> {<br /> base.OnNavigatedFrom(e);<br /> MySetting["name"] = this.txtName.Text;<br /> MySetting["pos"] = txtPos.Text;<br /> // 儲存<br /> MySetting.Save();<br /> }</p><p> protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)<br /> {<br /> base.OnNavigatedTo(e);<br /> // 當導航到頁面,讀入資料。<br /> if (MySetting.Contains("name"))<br /> {<br /> txtName.Text = MySetting["name"] as string;<br /> }<br /> if (MySetting.Contains("pos"))<br /> {<br /> txtPos.Text = MySetting["pos"] as string;<br /> }<br /> }<br /> }<br />}

 

好的,看,是不是很簡單?

隔離儲存就吹到這兒了,從下一篇文章開始,我們不玩抽象,我們來一些“所見即得”的東東,一起當一回山寨畫家,從下一節開始,我們一起討論圖形、畫刷、繪圖相關的內容。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.