Windows Phone 7 Belling‘s课堂(五) 独立存储空间(1)
基本功能:
允许你输入一个目录名称,点击“创建后”,将在隔离存储中创建一个目录,然后点击
第二个按钮,可以检测目录是否存在,第三个按钮用于删除目录。
前台UI代码:
View Code
1 <phone:PhoneApplicationPage
2 x:Class="shiyan3.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>
后台CS代码:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13 using Microsoft.Phone.Tasks;
14 using System.IO.IsolatedStorage;
15
16 namespace shiyan3
17 {
18 public partial class MainPage : PhoneApplicationPage
19 {
20 public MainPage()
21 {
22 InitializeComponent();
23 }
24
25 private void btnCreate_Click(object sender, RoutedEventArgs e)
26 {
27 // 通过GetUserStoreForApplication静态方法,可以返回一个IsolatedStorageFile实例。
28 IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
29 try
30 {
31 if (iso.DirectoryExists(txtDirName.Text)) //如果隔离存储空间中已经有此名字,return
32 {
33 MessageBox.Show("已经有了");
34 return;
35
36 }
37 iso.CreateDirectory(this.txtDirName.Text); //没有则创建
38 }
39 catch
40 { MessageBox.Show("Error !"); }
41 }
42
43 private void btnCheck_Click(object sender, RoutedEventArgs e)
44 {
45 IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
46 bool exists = iso.DirectoryExists(txtDirName.Text);
47 if (exists)
48 {
49 this.tbDisplay.Text = "已存在";
50 }
51 else
52 {
53 this.tbDisplay.Text = "不存在";
54 }
55 }
56
57 private void btnDel_Click(object sender, RoutedEventArgs e)
58 {
59 IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
60 try
61 {
62 iso.DeleteDirectory(txtDirName.Text);
63 }
64 catch
65 {
66 MessageBox.Show("Error !");
67 }
68 }
69
70
71
72 }
73 }
注意,要引入System.IO.IsolatedStorage命名空间。
界面展示:




View Code
浙公网安备 33010602011771号