Windows Phone開發(fā)(27):隔離存儲(chǔ)A
在很多資料或書籍上都翻譯為“獨(dú)立存儲(chǔ)”,不過,我想了一下,決定將IsolatedStorage翻譯為“隔離存儲(chǔ)”,我想這樣會(huì)更方便大家對(duì)這一概念的理解。
關(guān)于何為隔離存儲(chǔ),按照固有習(xí)慣,我不希望作太多理論上的解釋,一來理論化的東西容易把簡單的事情變得復(fù)雜化,二來,就算把理論知識(shí)說得有多***,相信大家都沒興趣看,就算你有興趣也會(huì)一頭霧水。
隔離存儲(chǔ)不是WP特有的,在Silverlight或WPF中也有,而且,更準(zhǔn)確地講,“獨(dú)立存儲(chǔ)”在.NET 2.0的時(shí)候已經(jīng)出現(xiàn),可能大家沒有注意到,不信?你可以在.NET類庫中找一下。
以前沒關(guān)注過也沒關(guān)系,隔離存儲(chǔ)其實(shí)是很簡單的,說白了就是Windows Phone上面的目錄和文件管理,在Windows平臺(tái)上,這些操作相信做過.NET開發(fā)的朋友們都肯定玩得很熟了。
Windows phone的目錄和文件管理方式與過去Windows CE或Windows Mobile是不同的,過去在這兩個(gè)OS上都是使用相對(duì)路徑,而其操作方法與PC系統(tǒng)相近;而WP則不同,盡管也是使用相對(duì)路徑,但操作方式和原理不同。
這就是我為什么要翻譯成“隔離存儲(chǔ)”了,這樣一來,大家從字面上就可以猜出它的特征了,每個(gè)應(yīng)用程序只能訪問其獨(dú)立的存儲(chǔ)空間,你不能去訪問其它應(yīng)用程序的目錄和結(jié)構(gòu),也不能訪問基礎(chǔ)操作系統(tǒng)的目錄和文件,這就大大提高了安全性。
好的,下面我們只需要一個(gè)簡單的示例,大家就會(huì)明白了。
示例允許你輸入一個(gè)目錄名稱,點(diǎn)擊“創(chuàng)建后”,將在隔離存儲(chǔ)中創(chuàng)建一個(gè)目錄,然后點(diǎn)擊第二個(gè)按鈕,可以檢測(cè)目錄是否存在,第三個(gè)按鈕用于刪除目錄。
- <phone:PhoneApplicationPage
- x:Class="IsoStorageSample1.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="隔離存儲(chǔ)" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - 在此處放置其他內(nèi)容-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <TextBox Height="72" HorizontalAlignment="Left" Margin="12,25,0,0" Name="txtDirName" VerticalAlignment="Top" Width="289" />
- <Button Content="創(chuàng)建" Height="72" HorizontalAlignment="Left" Margin="307,25,0,0" Name="btnCreate" VerticalAlignment="Top" Width="127" Click="btnCreate_Click" />
- <Button Content="檢測(cè)目錄是否已存在" Height="72" HorizontalAlignment="Left" Margin="146,120,0,0" Name="btnCheck" VerticalAlignment="Top" Width="276" Click="btnCheck_Click" />
- <Button Content="刪除目錄" Height="72" HorizontalAlignment="Left" Margin="0,283,0,0" Name="btnDel" VerticalAlignment="Top" Width="422" Click="btnDel_Click" />
- <TextBlock Height="38" HorizontalAlignment="Left" Margin="9,138,0,0" Name="tbDisplay" VerticalAlignment="Top" Width="131" />
- </Grid>
- </Grid>
- </phone:PhoneApplicationPage>
- private void btnCreate_Click(object sender, RoutedEventArgs e)
- {
- // 通過GetUserStoreForApplication靜態(tài)方法,可以返回一個(gè)IsolatedStorageFile實(shí)例。
- IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
- try
- {
- if (iso.DirectoryExists(txtDirName.Text))
- {
- return;
- }
- iso.CreateDirectory(this.txtDirName.Text);
- }
- catch
- { MessageBox.Show("Error !"); }
- }
- private void btnCheck_Click(object sender, RoutedEventArgs e)
- {
- IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
- bool exists = iso.DirectoryExists(txtDirName.Text);
- if (exists)
- {
- this.tbDisplay.Text="已存在";
- }
- else
- {
- this.tbDisplay.Text = "不存在";
- }
- }
- private void btnDel_Click(object sender, RoutedEventArgs e)
- {
- IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
- try
- {
- iso.DeleteDirectory(txtDirName.Text);
- }
- catch
- {
- MessageBox.Show("Error !");
- }
- }
注意,要引入System.IO.IsolatedStorage命名空間。