本文是隔離存儲的第三節(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ù)。
[html]?view plaincopyprint?
<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">??????????????<Grid?x:Name="LayoutRoot"?Background="Transparent">??????????<Grid.RowDefinitions>??????????????<RowDefinition?Height="Auto"/>??????????????<RowDefinition?Height="*"/>??????????</Grid.RowDefinitions>??????????????????????<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>??????????????????????<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>??
?
[csharp]?view plaincopyprint?
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?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;??????????????}??????????}??????}??}??
?
好的,看,是不是很簡單?
隔離存儲就吹到這兒了,從下一篇文章開始,我們不玩抽象,我們來一些“所見即得”的東東,一起當(dāng)一回山寨畫家,從下一節(jié)開始,我們一起討論圖形、畫刷、繪圖相關(guān)的內(nèi)容。
轉(zhuǎn)載于:https://www.cnblogs.com/songtzu/archive/2012/07/24/2607126.html
總結(jié)
以上是生活随笔為你收集整理的Windows Phone开发(29):隔离存储C 转:http://blog.csdn.net/tcjiaan/article/details/7447469...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。