Windows Phone开发之独立存储、文件的读写、ListBox绑定数据、NavigationService实现XAML跳转

独立存储:

IsolatedStorage独立存储空间是保存应用程序的一些数据已经配置文件,独立存储空间相对于其他的wp程序是独立的,也就是说每个wp程序都会有自己的独立存储空间,每个wp程序相互之间不能访问;

Isolated Storage又叫做隔离存储空间,Windows Phone 7手机上用来本地存储数据。下图是一个存储应用的文件夹结构图:

Isolated Storage用来创建与维护本地存储。WP7上的架构和Windows下的Silverlight类似,所有的读写操作都只限于隔离存储空间并且无法直接访问磁层操作系统的文件系统。这样能够防止非法的访问以及其他应用的破坏,增强安全性。

提示:如果你有两个应用想要共用一个同一个数据,则没法通过本地存储实现。你可以使用web服务等。

提示:WP7下的隔离存储空间没有配额的限制。应用应该只保存必要的数据。当Windows Phone只剩下10%的可用空间,用户会收到一个提醒并可能停止当前应用。对用户来讲这是一个很差的用户体验。

在隔离存储空间下可以进行目录操作、文件操作、应用程序配置信息等。

以上内容参考自:http://blog.csdn.net/shenzhoulong001/article/details/7452194

实例内容:
界面ListBox上动态绑定数据,ListBoxItem为超链接HyperlinkButton,内容是文件名,可以连接到其他XAML显示出来。
Save按钮功能是创建文件列表。MainPanel可以在Load时加载显示出来。

界面UI代码:
<Grid x:Name="Root"
          VerticalAlignment="Stretch"
          HorizontalAlignment="Stretch" 
          Loaded="Root_Loaded">
        <Button Name="button1"
                Margin="8,300,332,392"
                Content="Save"
                Click="button1_Click" />
        <ListBox Height="228" 
                 HorizontalAlignment="Left"
                 Margin="12,417,0,0" 
                 Name="listBox1" 
                 VerticalAlignment="Top" 
                 Width="460">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel HorizontalAlignment="Stretch"
                                  VerticalAlignment="Stretch">
                     <HyperlinkButton HorizontalAlignment="Left"
                                       VerticalAlignment="Top"
                                       Width="300"
                                       Height="72"
                                       Content="{Binding}"
                                       Click="mylistBoxItem_Click"
                                       />
                    </StackPanel>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox> 
    </Grid>

 解释:
 在ListBox上动态加载数据显示时,必须使用以下代码,
 <ListBox.ItemTemplate>
    <DataTemplate>               
                </DataTemplate>
            </ListBox.ItemTemplate>
 Content="{Binding}"表示该内容是绑定的数据。 

-----------------------------------------------------------------------------
Save按钮事件:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            newFile("test1.txt", "这是第一个文件!");
            newFile("test2.txt", "这是第二个文件!");
            newFile("test3.txt", "这是第三个文件!");
            //添加绑定
            var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
            string[] fileNames= appStorage.GetFileNames();
            this.listBox1.ItemsSource = fileNames;
           
        }
		
		 public void newFile(string fileName, string content) {
            //新建文件
            var appStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
			
            try {
			if (!appStorage.FileExists(fileName)) {
                using ( var file=appStorage.CreateFile(fileName)){
                    using (var writer = new StreamWriter(file)){
                        writer.Write(content);
                    }
                  }
				}
            }catch (Exception ee){
                MessageBox.Show(ee.ToString());
            }
        }

----------------------------------------------------------------------

HyperLinkButton事件:

 private void mylistBoxItem_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton myLictBoxItem = (HyperlinkButton)sender;
            string uri = string.Format("/PhoneApp7;component/Page.xaml?id={0}",myLictBoxItem.Content);
            NavigationService.Navigate(new Uri(uri, UriKind.Relative));
        }

解释:
NavigationService实现了Uri的定义与跳转

----------------------------------------------------------------------
新界面加载数据:

 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string fileName = this.NavigationContext.QueryString["id"];
            //读出文件的内容
            IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();           
            if (appStorage.FileExists(fileName)) {
                using (var file = appStorage.OpenFile(fileName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)){
                    using (var reader = new StreamReader(file)){
                        textBlock1.Text = fileName;
                        textBlock2.Text = reader.ReadToEnd();
                    }
                }
            }

        }




 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值