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

来源:互联网 发布:射频发生器调谐网络 编辑:程序博客网 时间:2024/06/04 00:24

在很多资料或书籍上都翻译为“独立存储”,不过,我想了一下,决定将IsolatedStorage翻译为“隔离存储”,我想这样会更方便大家对这一概念的理解。
关于何为隔离存储,按照固有习惯,我不希望作太多理论上的解释,一来理论化的东西容易把简单的事情变得复杂化,二来,就算把理论知识说得有多完美,相信大家都没兴趣看,就算你有兴趣也会一头雾水。

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

以前没关注过也没关系,隔离存储其实是很简单的,说白了就是Windows Phone上面的目录和文件管理,在Windows平台上,这些操作相信做过.NET开发的朋友们都肯定玩得很熟了。

Windows phone的目录和文件管理方式与过去Windows CE或Windows Mobile是不同的,过去在这两个OS上都是使用相对路径,而其操作方法与PC系统相近;而WP则不同,尽管也是使用相对路径,但操作方式和原理不同。

这就是我为什么要翻译成“隔离存储”了,这样一来,大家从字面上就可以猜出它的特征了,每个应用程序只能访问其独立的存储空间,你不能去访问其它应用程序的目录和结构,也不能访问基础操作系统的目录和文件,这就大大提高了安全性。

好的,下面我们只需要一个简单的示例,大家就会明白了。
示例允许你输入一个目录名称,点击“创建后”,将在隔离存储中创建一个目录,然后点击第二个按钮,可以检测目录是否存在,第三个按钮用于删除目录。

 

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


 

        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命名空间。

 

 

 

原创粉丝点击