Windows Phone开发(27):隔离存储A

移动开发
隔离存储不是WP特有的,在Silverlight或WPF中也有,而且,更准确地讲,“独立存储”在.NET 2.0的时候已经出现,可能大家没有注意到,不信?你可以在.NET类库中找一下。

在很多资料或书籍上都翻译为“独立存储”,不过,我想了一下,决定将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.     <!--LayoutRoot 是包含所有页面内容的根网格--> 
  16.     <Grid x:Name="LayoutRoot" Background="Transparent"
  17.         <Grid.RowDefinitions> 
  18.             <RowDefinition Height="Auto"/> 
  19.             <RowDefinition Height="*"/> 
  20.         </Grid.RowDefinitions> 
  21.         <!--TitlePanel 包含应用程序的名称和页标题--> 
  22.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"
  23.             <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/> 
  24.             <TextBlock x:Name="PageTitle" Text="隔离存储" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  25.         </StackPanel> 
  26.         <!--ContentPanel - 在此处放置其他内容--> 
  27.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"
  28.             <TextBox Height="72" HorizontalAlignment="Left" Margin="12,25,0,0" Name="txtDirName" VerticalAlignment="Top" Width="289" /> 
  29.             <Button Content="创建" Height="72" HorizontalAlignment="Left" Margin="307,25,0,0" Name="btnCreate" VerticalAlignment="Top" Width="127" Click="btnCreate_Click" /> 
  30.             <Button Content="检测目录是否已存在" Height="72" HorizontalAlignment="Left" Margin="146,120,0,0" Name="btnCheck" VerticalAlignment="Top" Width="276" Click="btnCheck_Click" /> 
  31.             <Button Content="删除目录" Height="72" HorizontalAlignment="Left" Margin="0,283,0,0" Name="btnDel" VerticalAlignment="Top" Width="422" Click="btnDel_Click" /> 
  32.             <TextBlock Height="38" HorizontalAlignment="Left" Margin="9,138,0,0" Name="tbDisplay" VerticalAlignment="Top" Width="131" /> 
  33.         </Grid> 
  34.     </Grid> 
  35. </phone:PhoneApplicationPage>
  1. private void btnCreate_Click(object sender, RoutedEventArgs e) 
  2.     // 通过GetUserStoreForApplication静态方法,可以返回一个IsolatedStorageFile实例。 
  3.     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 
  4.     try 
  5.     { 
  6.         if (iso.DirectoryExists(txtDirName.Text)) 
  7.         { 
  8.             return
  9.         } 
  10.         iso.CreateDirectory(this.txtDirName.Text); 
  11.     } 
  12.     catch 
  13.     { MessageBox.Show("Error !"); } 
  14. private void btnCheck_Click(object sender, RoutedEventArgs e) 
  15.     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 
  16.     bool exists = iso.DirectoryExists(txtDirName.Text); 
  17.     if (exists) 
  18.     { 
  19.         this.tbDisplay.Text="已存在"
  20.     } 
  21.     else 
  22.     { 
  23.         this.tbDisplay.Text = "不存在"
  24.     } 
  25. private void btnDel_Click(object sender, RoutedEventArgs e) 
  26.     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 
  27.     try 
  28.     { 
  29.         iso.DeleteDirectory(txtDirName.Text); 
  30.     } 
  31.     catch 
  32.     { 
  33.         MessageBox.Show("Error !"); 
  34.     } 

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

责任编辑:闫佳明 来源: oschina
相关推荐

2013-04-19 16:05:52

Windows PhoWindows Pho

2013-04-19 16:14:36

Windows PhoWindows Pho

2014-12-22 14:21:57

Windows Pho隔离式存储机制

2012-08-01 10:26:33

Windows Pho

2013-07-30 12:37:56

Windows PhoWindows Pho

2010-04-21 17:07:54

Windows Pho

2010-12-01 09:01:31

独立存储Windows Pho

2011-06-07 12:42:15

Windows Pho

2013-04-17 14:00:06

Windows PhoWindows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2013-04-19 16:34:56

Windows PhoWindows Pho

2013-04-16 17:02:50

Windows Pho概论

2010-04-08 17:40:23

Windows Pho

2013-04-17 14:47:19

Windows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho

2013-07-31 13:03:51

Windows PhoWindows Pho

2010-07-16 15:29:02

Windows Pho

2012-08-16 10:35:50

Windows Pho

2013-04-17 13:27:04

Windows PhoWindows Pho

2010-12-14 18:48:49

微软
点赞
收藏

51CTO技术栈公众号