主要的三个步骤

1.调用手机的独立存储

  例如:IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()

2.创建独立存储文件流

  例如:IsolatedStorageFileStream location = new IsolatedStorageFileStream(nateText.Text + ".item", System.IO.FileMode.Create, storage);

3.读写该文件流

  例如:将独立存储文件流转化为可写流

       System.IO.StreamWriter file = new System.IO.StreamWriter(location);

      将XML文件保存到流file上,即已经写入到手机独立存储文件上,_doc是用户创建的文件:

      _doc.Save(file);

      转化为可读流:

      System.IO.StreamReader file = new System.IO.StreamReader(location);

      解析流,转化为XML

      _xml = XElement.Parse(file.ReadToEnd());

下面给出一个购物清单的例子

清单列表界面:

 <Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel Grid.Row="" Margin="12,17,0,28">
<TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="购物清单" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel> <!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="" Margin="12,0,12,0">
<ListBox Margin="0,0,38,131" Name="Files"/>
<Button Content="添加" Name="btn_add" HorizontalAlignment="Left" Height="" Margin="11,493,0,0" VerticalAlignment="Top" Width=""/>
</Grid>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage; namespace PhoneApp1
{
public partial class IsoFiles : PhoneApplicationPage
{
public IsoFiles()
{
InitializeComponent();
BindList();
btn_add.Click += btn_add_Click;
} void btn_add_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/IsoFileAdd.xaml",UriKind.Relative));
} private void BindList()
{
Files.Items.Clear();//先清空一下ListBox数据
//获取应用程序的独立存储文件
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
//获取并循环*.item的存储文件
foreach (string filename in storage.GetFileNames("*.item"))
{
//动态构建一个Grid
Grid grid = new Grid();
//定义第一列
ColumnDefinition co1 = new ColumnDefinition();
GridLength gl = new GridLength();
co1.Width = gl;
grid.ColumnDefinitions.Add(co1);
//定义第二列
ColumnDefinition co2 = new ColumnDefinition();
co2.Width = gl;
grid.ColumnDefinitions.Add(co2);
//添加一个TextBlock显示文件名到第一列
TextBlock tblock = new TextBlock();
tblock.Text = filename;
Grid.SetColumn(tblock, );
//添加一个HyperlinkButton连接到购物详细清单页面,这是第二列
HyperlinkButton hybtn = new HyperlinkButton();
hybtn.Content = "详细信息";
GridLength glth = new GridLength();
hybtn.Width = ;
hybtn.NavigateUri = new Uri("/IsoFilePage.xaml?name="+filename, UriKind.Relative);//传递文件名称到商品详细页面
Grid.SetColumn(hybtn, );
grid.Children.Add(tblock);
grid.Children.Add(hybtn);
Files.Items.Add(grid);
}
}
}
}
}

添加商品界面:

<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel Grid.Row="" Margin="12,17,0,28">
<TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="添加商品" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel> <!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="" Margin="12,0,12,0">
<TextBlock HorizontalAlignment="Left" Height="" Margin="23,52,0,0" TextWrapping="Wrap" Text="名称" VerticalAlignment="Top" Width=""/>
<TextBox Name="MC" HorizontalAlignment="Left" Height="" Margin="107,36,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="" FontSize=""/>
<TextBlock HorizontalAlignment="Left" Height="" Margin="23,121,0,0" TextWrapping="Wrap" Text="价格" VerticalAlignment="Top" Width=""/>
<TextBox Name="JG" HorizontalAlignment="Left" Height="" Margin="107,105,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="" FontSize=""/>
<TextBlock HorizontalAlignment="Left" Height="" Margin="23,185,0,0" TextWrapping="Wrap" Text="数量" VerticalAlignment="Top" Width=""/>
<TextBox Name="SL" HorizontalAlignment="Left" Height="" Margin="107,169,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="" FontSize=""/>
<Button Content="保存" Name="btn_add" HorizontalAlignment="Left" Height="" Margin="257,411,0,0" VerticalAlignment="Top" Width=""/> </Grid>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Xml.Linq; namespace PhoneApp1
{
public partial class IsoFileAdd : PhoneApplicationPage
{
public IsoFileAdd()
{
InitializeComponent();
btn_add.Click += btn_add_Click;
} void btn_add_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
XDocument _doc = new XDocument();
XElement _item = new XElement(MC.Text);//创建一个XML元素
XAttribute price = new XAttribute("price", JG.Text);//创建一个XML属性
XAttribute count = new XAttribute("count", SL.Text);
_item.Add(price, count);//将这两个属性添加到XML元素上
//用_item新建一个XML的Linq文档
_doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), _item);
//创建一个独立存储的文件流
IsolatedStorageFileStream location = new IsolatedStorageFileStream(MC.Text + ".item", System.IO.FileMode.Create, storage);
//将独立存储文件流转化为可写流
System.IO.StreamWriter file = new System.IO.StreamWriter(location);
//将XML文件保存到file上,即已经写入到手机独立存储文件上
_doc.Save(file);
file.Dispose();//关闭可写流
location.Dispose();//关闭手机独立存储流
//调回清单主页
NavigationService.Navigate(new Uri("/IsoFiles.xaml", UriKind.Relative)); } }
}
}

商品详细界面:

<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel Grid.Row="" Margin="12,17,0,28">
<TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="详细信息" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel> <!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="63*"/>
<RowDefinition Height="77*"/>
<RowDefinition Height="467*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="19*"/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left" Height="" Margin="22,11,0,0" TextWrapping="Wrap" Text="名称" VerticalAlignment="Top" Width="" Grid.ColumnSpan=""/>
<TextBlock Name="MC" Grid.Column="" HorizontalAlignment="Left" Height="" Margin="10,11,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width=""/>
<TextBlock HorizontalAlignment="Left" Height="" Margin="22,10,0,0" TextWrapping="Wrap" Text="价格" VerticalAlignment="Top" Width="" Grid.Row="" Grid.ColumnSpan=""/>
<TextBlock x:Name="JG" Grid.Column="" HorizontalAlignment="Left" Height="" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="" Grid.Row=""/>
<TextBlock HorizontalAlignment="Left" Height="" Margin="22,10,0,0" TextWrapping="Wrap" Text="数量" VerticalAlignment="Top" Width="" Grid.Row="" Grid.ColumnSpan=""/>
<TextBlock x:Name="SL" Grid.Column="" HorizontalAlignment="Left" Height="" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="" Grid.Row=""/>
<Button Content="返回购物清单" Name="btn_back" Grid.Column="" HorizontalAlignment="Left" Height="" Margin="149,379,0,0" Grid.Row="" VerticalAlignment="Top" Width=""/> </Grid>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Xml.Linq; namespace PhoneApp1
{
public partial class IsoFilePage : PhoneApplicationPage
{
public IsoFilePage()
{
InitializeComponent();
btn_back.Click += btn_back_Click;
} void btn_back_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/IsoFiles.xaml", UriKind.Relative));
}
//OnNavigatedTo事件是当跳转到当前的页面的时候触发的
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string itemName = "";
//获取上一页面传过来的item值
bool itemExists = NavigationContext.QueryString.TryGetValue("name", out itemName);
if (itemExists)
{
MC.Text = itemName;
}
using (IsolatedStorageFile storage=IsolatedStorageFile.GetUserStoreForApplication())
{
XElement _xml;//定义Linq的XML元素
//打开独立存储文件
IsolatedStorageFileStream location = new IsolatedStorageFileStream(itemName, System.IO.FileMode.Open, storage);
//转化为可读流
System.IO.StreamReader file = new System.IO.StreamReader(location);
//解析流转化为XML
_xml = XElement.Parse(file.ReadToEnd());
if (_xml.Name.LocalName != null)
{
XAttribute price = _xml.Attribute("price");//获取价格
JG.Text = price.Value.ToLower();
XAttribute count = _xml.Attribute("count");//获取数量
SL.Text = count.Value.ToLower();
MC.Text = itemName;
}
file.Dispose();
location.Dispose();
}
base.OnNavigatedTo(e);
}
}
}

WP8数据存储--独立存储文件的更多相关文章

  1. wp8数据存储--独立存储文件 【转】

    出自 : http://www.cnblogs.com/MyBeN/p/3339019.html 文章篇幅有点大,建议去源网看看 1.调用手机的独立存储 例如:IsolatedStorageFile ...

  2. WP8数据存储--独立存储设置

    <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinition ...

  3. Android - 数据存储 -存储文件

    Android使用的文件系统和其他平台的基本磁盘的文件系统很相似.这里将要介绍如何使用File API在Android文件系统中读写文件. File对象适合按顺序读写大量的数据.例如,适合图片文件或者 ...

  4. Android - 数据存储 -存储键值对

    如果你有少量的键值数据需要存储,可以使用SharedPreferencesAPI.SharedPreferences对象指向一个包含键值对的文件并且提供了一些简单的方法来读取它们.每个SharedPr ...

  5. WP8 SqlCE和SqlLite数据存储性能比较

    在平时的开发中一定会用到本地数据存储,除了独立存储外我们还可以选择SqlCE和SqlLite:于是在选择上我们就必须权衡他们两者的性能择优选择. 测试代码:(这个例子是在msdn sqllite例子上 ...

  6. wp8.1 Study10:APP数据存储

    一.理论 1.App的各种数据在WP哪里的? 下图很好介绍了这个问题.有InstalltionFolder, knownFolder, SD Card... 2.一个App的数据存储概览 主要分两大部 ...

  7. Android 数据存储五种方式

    1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...

  8. 【读书笔记《Android游戏编程之从零开始》】20.游戏开发基础(游戏数据存储)

    对于数据的存储,Android 提供了4种保存方式. (1)SharedPreference 此方法适用于简单数据的保持,文如其名,属于配置性质的保存,不适合比较大的情况,默认存放在手机内存里 (2) ...

  9. Android数据存储三剑客——SharedPreferences、File、SQLite

    Android中常用的数据存储一般有三种方式:SharedPreferences.文件和SQLite数据库,用来保存需要长时间保存的数据.本文将通过几个具体的小实例来讲解这三种方式的具体实现. 数据存 ...

随机推荐

  1. C#关于log4net(Log For Net)

    1       介绍 log4net(Log For Net)是Apache开源的应用于.Net框架的日志记录工具,详细信息参见Apache网站.它是针对Java的log4j(Log For Java ...

  2. (十四)基于GTID的主从复制

    (1)GTID主从复制 1)环境介绍 /etc/redhat-release CentOS Linux release 7.3.1611 (Core) MySQL版本:5.7 mysql> se ...

  3. c# 使用ssh连接远程主机(ssh.net演示)

    本教程使用的是ssh.net这个库.项目地址:https://github.com/sshnet/SSH.NET 使用ssh客户端连接远程主机执行命令,并拿到输出结果: using (var sshC ...

  4. 百度之星资格赛 2016 Problem 1002

    本文链接:http://www.cnblogs.com/Ash-ly/p/5494623.html 题意: 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个 ...

  5. 51nod 更难的矩阵取数问题(动态规划)

    更难的矩阵取数问题 给定一个m行n列的矩阵,矩阵每个元素是一个正整数,你现在 在左上角(第一行第一列),你需要走到右下角(第m行,第n列),每次只能朝右或者下走到相邻的位置,不能走出矩阵.然后再从右下 ...

  6. 前端面试题 vue

    webpack 作用:webpack是把项目当作一个整体,通过一个给定的的主文件,webpack将从这个文件开始找到你的项目的所有依赖文件,使用loaders处理它们,最后打包成一个或多个浏览器可识别 ...

  7. date conversion

    SELECT to_char(sysdate,'yyyymmdd hh:mi:ss'), to_char(sysdate ,'yyyymmdd hh:mi:ss'), to_char(sysdate ...

  8. 算法-插入排序(Insertion sorting)

    本文由@呆代待殆原创,转载请注明出处. 简介:这是一个比较算法,形象的描述插入算法就和我们玩扑克的时候我们排列手牌的方式是一样的,最开始我们手上什么都没有,然后我们每摸一张牌就把它插入到正确的位置,直 ...

  9. 面试问题Distilled

    1. 你在项目里都用过哪些Spring的组件 2. Spring AOP的实现原理 3. Hibernate的乐观锁和悲观锁 4. Hibernate的缓存机制 5. 对SOA的了解和认识 6. 谈谈 ...

  10. [BZOJ2876]骑行川藏

    以前并没有发现微积分教材上有这种东西...我还是太菜了... 其实就是要在满足$\sum\limits_{i=1}^nk_is_i(v_i-v_i')^2\leq E$的同时求$\sum\limits ...