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

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

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

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

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

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

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

 

  1. <phone:PhoneApplicationPage   
  2.     x:Class="IsoStorageSample1.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"  
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.     Foreground="{StaticResource PhoneForegroundBrush}"  
  13.     SupportedOrientations="Portrait" Orientation="Portrait"  
  14.     shell:SystemTray.IsVisible="True">  
  15.   
  16.     <!--LayoutRoot 是包含所有頁面內容的根網格-->  
  17.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  18.         <Grid.RowDefinitions>  
  19.             <RowDefinition Height="Auto"/>  
  20.             <RowDefinition Height="*"/>  
  21.         </Grid.RowDefinitions>  
  22.   
  23.         <!--TitlePanel 包含應用程序的名稱和頁標題-->  
  24.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  25.             <TextBlock x:Name="ApplicationTitle" Text="我的應用程序" Style="{StaticResource PhoneTextNormalStyle}"/>  
  26.             <TextBlock x:Name="PageTitle" Text="隔離存儲" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  27.         </StackPanel>  
  28.   
  29.         <!--ContentPanel - 在此處放置其他內容-->  
  30.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  31.             <TextBox Height="72" HorizontalAlignment="Left" Margin="12,25,0,0" Name="txtDirName" VerticalAlignment="Top" Width="289" />  
  32.             <Button Content="創建" Height="72" HorizontalAlignment="Left" Margin="307,25,0,0" Name="btnCreate" VerticalAlignment="Top" Width="127" Click="btnCreate_Click" />  
  33.             <Button Content="檢測目錄是否已存在" Height="72" HorizontalAlignment="Left" Margin="146,120,0,0" Name="btnCheck" VerticalAlignment="Top" Width="276" Click="btnCheck_Click" />  
  34.             <Button Content="刪除目錄" Height="72" HorizontalAlignment="Left" Margin="0,283,0,0" Name="btnDel" VerticalAlignment="Top" Width="422" Click="btnDel_Click" />  
  35.             <TextBlock Height="38" HorizontalAlignment="Left" Margin="9,138,0,0" Name="tbDisplay" VerticalAlignment="Top" Width="131" />  
  36.         </Grid>  
  37.     </Grid>  
  38.    
  39.   
  40. </phone:PhoneApplicationPage>  
<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 是包含所有頁面內容的根網格-->
    <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">
            <TextBox Height="72" HorizontalAlignment="Left" Margin="12,25,0,0" Name="txtDirName" VerticalAlignment="Top" Width="289" />
            <Button Content="創建" Height="72" HorizontalAlignment="Left" Margin="307,25,0,0" Name="btnCreate" VerticalAlignment="Top" Width="127" Click="btnCreate_Click" />
            <Button Content="檢測目錄是否已存在" 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>


 

  1. private void btnCreate_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     // 通過GetUserStoreForApplication靜態方法,可以返回一個IsolatedStorageFile實例。   
  4.     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();  
  5.     try  
  6.     {  
  7.         if (iso.DirectoryExists(txtDirName.Text))  
  8.         {  
  9.             return;  
  10.         }  
  11.         iso.CreateDirectory(this.txtDirName.Text);  
  12.     }  
  13.     catch  
  14.     { MessageBox.Show("Error !"); }  
  15. }  
  16.   
  17. private void btnCheck_Click(object sender, RoutedEventArgs e)  
  18. {  
  19.     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();  
  20.     bool exists = iso.DirectoryExists(txtDirName.Text);  
  21.     if (exists)  
  22.     {  
  23.         this.tbDisplay.Text="已存在";  
  24.     }  
  25.     else  
  26.     {  
  27.         this.tbDisplay.Text = "不存在";  
  28.     }  
  29. }  
  30.   
  31. private void btnDel_Click(object sender, RoutedEventArgs e)  
  32. {  
  33.     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();  
  34.     try  
  35.     {  
  36.         iso.DeleteDirectory(txtDirName.Text);  
  37.     }  
  38.     catch  
  39.     {  
  40.         MessageBox.Show("Error !");  
  41.     }  
  42. }  
        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            // 通過GetUserStoreForApplication靜態方法,可以返回一個IsolatedStorageFile實例。
            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命名空間。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章