Windows Phone開發(27):隔離儲存A

來源:互聯網
上載者:User

在很多資料或書籍上都翻譯為“隔離儲存區 (Isolated Storage)”,不過,我想了一下,決定將IsolatedStorage翻譯為“隔離儲存”,我想這樣會更方便大家對這一概念的理解。
關於何為隔離儲存,按照固有習慣,我不希望作太多理論上的解釋,一來理論化的東西容易把簡單的事情變得複雜化,二來,就算把理論知識說得有多完美,相信大家都沒興趣看,就算你有興趣也會一頭霧水。

隔離儲存不是WP特有的,在Silverlight或WPF中也有,而且,更準確地講,“隔離儲存區 (Isolated Storage)”在.NET 2.0的時候已經出現,可能大家沒有注意到,不信?你可以在.NET類庫中找一下。

以前沒關注過也沒關係,隔離儲存其實是很簡單的,說白了就是Windows Phone上面的目錄和檔案管理,在Windows平台上,這些操作相信做過.NET開發的朋友們都肯定玩得很熟了。

Windows phone的目錄和檔案管理方式與過去Windows CE或Windows Mobile是不同的,過去在這兩個OS上都是使用相對路徑,而其操作方法與PC系統相近;而WP則不同,儘管也是使用相對路徑,但操作方式和原理不同。

這就是我為什麼要翻譯成“隔離儲存”了,這樣一來,大家從字面上就可以猜出它的特徵了,每個應用程式只能訪問其獨立的儲存空間,你不能去訪問其它應用程式的目錄和結構,也不能訪問基礎作業系統的目錄和檔案,這就大大提高了安全性。

好的,下面我們只需要一個簡單的樣本,大家就會明白了。
樣本允許你輸入一個目錄名稱,點擊“建立後”,將在隔離儲存中建立一個目錄,然後點擊第二個按鈕,可以檢測目錄是否存在,第三個按鈕用於刪除目錄。

 

<phone:PhoneApplicationPage<br /> x:Class="IsoStorageSample1.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 /> <TextBox Height="72" HorizontalAlignment="Left" Margin="12,25,0,0" Name="txtDirName" VerticalAlignment="Top" Width="289" /><br /> <Button Content="建立" Height="72" HorizontalAlignment="Left" Margin="307,25,0,0" Name="btnCreate" VerticalAlignment="Top" Width="127" Click="btnCreate_Click" /><br /> <Button Content="檢測目錄是否已存在" Height="72" HorizontalAlignment="Left" Margin="146,120,0,0" Name="btnCheck" VerticalAlignment="Top" Width="276" Click="btnCheck_Click" /><br /> <Button Content="刪除目錄" Height="72" HorizontalAlignment="Left" Margin="0,283,0,0" Name="btnDel" VerticalAlignment="Top" Width="422" Click="btnDel_Click" /><br /> <TextBlock Height="38" HorizontalAlignment="Left" Margin="9,138,0,0" Name="tbDisplay" VerticalAlignment="Top" Width="131" /><br /> </Grid><br /> </Grid></p><p></phone:PhoneApplicationPage>

 

private void btnCreate_Click(object sender, RoutedEventArgs e)<br /> {<br /> // 通過GetUserStoreForApplication靜態方法,可以返回一個IsolatedStorageFile執行個體。<br /> IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();<br /> try<br /> {<br /> if (iso.DirectoryExists(txtDirName.Text))<br /> {<br /> return;<br /> }<br /> iso.CreateDirectory(this.txtDirName.Text);<br /> }<br /> catch<br /> { MessageBox.Show("Error !"); }<br /> }</p><p> private void btnCheck_Click(object sender, RoutedEventArgs e)<br /> {<br /> IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();<br /> bool exists = iso.DirectoryExists(txtDirName.Text);<br /> if (exists)<br /> {<br /> this.tbDisplay.Text="已存在";<br /> }<br /> else<br /> {<br /> this.tbDisplay.Text = "不存在";<br /> }<br /> }</p><p> private void btnDel_Click(object sender, RoutedEventArgs e)<br /> {<br /> IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();<br /> try<br /> {<br /> iso.DeleteDirectory(txtDirName.Text);<br /> }<br /> catch<br /> {<br /> MessageBox.Show("Error !");<br /> }<br /> }<br />

 

注意,要引入System.IO.IsolatedStorage命名空間。

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.