zoukankan      html  css  js  c++  java
  • Windows Phone开发(27):隔离存储A 转:http://blog.csdn.net/tcjiaan/article/details/7425212

    在很多资料或书籍上都翻译为“独立存储”,不过,我想了一下,决定将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>  


     

    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. }  


     

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

  • 相关阅读:
    ssm依赖
    NSNotificationCenter详解
    Objective-C语法之代码块(block)的使用
    IOS UI UITableView
    IOS 多线程(4) --线程通讯
    IOS 多线程(3) --线程安全
    IOS 多线程(2) --NSThread
    IOS 多线程(1) --基础知识
    IOS UI TextFiled常用总结
    IOS UI TabBar标签栏的使用
  • 原文地址:https://www.cnblogs.com/songtzu/p/2607133.html
Copyright © 2011-2022 走看看