XML结构清晰,使用手机独立存储的时候可以利用上XML的文件结构来保存信息,这是一种不错的选择。
使用IsolatedStorageFile对象来实现手机信息的存储,有三个主要步骤,
1、调用手机的独立存储
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()
2、创建独立存储文件流
IsolatedStorageFileStream location = new IsolatedStorageFileStream(nameTxt.Text + ".item", System.IO.FileMode.Create, storage);
3、读写该文件流
写:
//将本地存储文件流转化为可写流
System.IO.StreamWriter file = new System.IO.StreamWriter(location);
//将XML文件 保存到流file上 即已经写入到手机本地存储文件上
_doc.Save(file);   //_doc是你创建的文件
读:
//转化为可读流
System.IO.StreamReader file = new System.IO.StreamReader(location);
//解析流 转化为XML
_xml = XElement.Parse(file.ReadToEnd());
下面是一个Demo购物清单
2011040121341221.jpg

清单列表
MainPage.xaml
  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 System.IO.IsolatedStorage;
  14. using System.Xml.Linq;

  15. namespace ShoppingList_Demo
  16. {
  17.     public partial class MainPage : PhoneApplicationPage
  18.     {
  19.         public MainPage()
  20.         {
  21.             InitializeComponent();
  22.             //加载页面触发Loaded事件
  23.             Loaded += (object sender, RoutedEventArgs e) =>
  24.             {
  25.                 Files.Items.Clear();//先清空一下ListBox的数据
  26.                 //获取应用程序的本地存储文件
  27.                 using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
  28.                 {
  29.                     //获取并循环 *.item的存储文件
  30.                     foreach (string filename in storage.GetFileNames("*.item"))
  31.                     {
  32.                         //动态构建一个Grid
  33.                         Grid a = new Grid();
  34.                         //定义第一列
  35.                         ColumnDefinition col = new ColumnDefinition();
  36.                         GridLength gl = new GridLength(200);
  37.                         col.Width = gl;
  38.                         a.ColumnDefinitions.Add(col);
  39.                         //定义第二列
  40.                         ColumnDefinition col2 = new ColumnDefinition();
  41.                         GridLength gl2 = new GridLength(200);
  42.                         col2.Width = gl;
  43.                         a.ColumnDefinitions.Add(col2);
  44.                         //添加一个TextBlock现实文件名 到第一列
  45.                         TextBlock txbx = new TextBlock();
  46.                         txbx.Text = filename;
  47.                         Grid.SetColumn(txbx, 0);
  48.                         //添加一个HyperlinkButton链接到购物详细清单页面 这是第二列
  49.                         HyperlinkButton btn = new HyperlinkButton();
  50.                         btn.Width = 200;
  51.                         btn.Content = "查看详细";
  52.                         btn.Name = filename;
  53.                         btn.NavigateUri = new Uri("/DisplayPage.xaml?item=" + filename, UriKind.Relative);//传递文件名到商品详细页面

  54.                         Grid.SetColumn(btn, 1);
  55.                         
  56.                         a.Children.Add(txbx);
  57.                         a.Children.Add(btn);

  58.                         Files.Items.Add(a);
  59.                     }
  60.                 }
  61.             };

  62.         }
  63.         private void New_Click(object sender, EventArgs e)
  64.         {
  65.             NavigationService.Navigate(new Uri("/AddItem.xaml", UriKind.Relative));
  66.         }
  67.     }
  68. }
复制代码
2011040121334893.jpg

AddItem.xaml
  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 System.IO.IsolatedStorage;
  14. using System.Xml.Linq;

  15. namespace ShoppingList_Demo
  16. {
  17.     public partial class AddItem : PhoneApplicationPage
  18.     {
  19.         public AddItem()
  20.         {
  21.             InitializeComponent();
  22.         }

  23.         private void BtnSave_Click(object sender, RoutedEventArgs e)
  24.         {
  25.             using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
  26.             {
  27.                 XDocument _doc = new XDocument();

  28.                 XElement _item = new XElement(nameTxt.Text);//创建一个XML元素
  29.                 XAttribute price = new XAttribute("price", priceTxt.Text);//创建一个XML属性
  30.                 XAttribute quantity = new XAttribute("quantity", quanTxt.Text);

  31.                 _item.Add(price, quantity);//将这两个属性添加到 XML元素上
  32.                 //用_item 新建一个XML的Linq文档
  33.                 _doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), _item);

  34.                 //创建一个本地存储的文件流
  35.                 IsolatedStorageFileStream location = new IsolatedStorageFileStream(nameTxt.Text + ".item",
  36.                         System.IO.FileMode.Create, storage);
  37.                 //将本地存储文件流转化为可写流
  38.                 System.IO.StreamWriter file = new System.IO.StreamWriter(location);
  39.                 //将XML文件 保存到流file上 即已经写入到手机本地存储文件上
  40.                 _doc.Save(file);

  41.                 file.Dispose();//关闭可写流
  42.                 location.Dispose();//关闭手机本地存储流
  43.                 //调回清单主页
  44.                 NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
  45.             }
  46.         }
  47.     }
  48. }
复制代码
2011040121353748.jpg

查看商品详细
DisplayPage.xaml
  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 System.IO.IsolatedStorage;
  14. using System.Xml.Linq;
  15. using System.Windows.Navigation;

  16. namespace ShoppingList_Demo
  17. {
  18.     public partial class DisplayPage : PhoneApplicationPage
  19.     {
  20.         
  21.         public DisplayPage()
  22.         {
  23.             InitializeComponent();
  24.         }

  25.         protected override void OnNavigatedTo(NavigationEventArgs e)
  26.         {
  27.             String itemName = "";

  28.             base.OnNavigatedTo(e);
  29.             //获取上一页面传递过来的item值
  30.             bool itemExists = NavigationContext.QueryString.TryGetValue("item", out itemName);

  31.             if (itemExists)
  32.             {
  33.                 PageTitle.Text = itemName;
  34.             }

  35.             using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
  36.             {
  37.                 XElement _xml;//定义Linq的XML元素
  38.                 //打开本地存储文件
  39.                 IsolatedStorageFileStream location = new IsolatedStorageFileStream(itemName, System.IO.FileMode.Open, storage);
  40.                 //转化为可读流
  41.                 System.IO.StreamReader file = new System.IO.StreamReader(location);
  42.                 //解析流 转化为XML
  43.                 _xml = XElement.Parse(file.ReadToEnd());

  44.                 if (_xml.Name.LocalName != null)
  45.                 {
  46.                     XAttribute priceTemp = _xml.Attribute("price");//获取价格
  47.                     priceTxt.Text = priceTemp.Value.ToLower();
  48.                     XAttribute quanTemp = _xml.Attribute("quantity");//获取数量
  49.                     quanTxt.Text = quanTemp.Value.ToLower();
  50.                     nameTxt.Text = itemName;
  51.                 }

  52.                 file.Dispose();
  53.                 location.Dispose();
  54.             }
  55.         }

  56.         private void BtnBack_Click(object sender, RoutedEventArgs e)
  57.         {
  58.             NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
  59.         }
  60.     }
  61. }
复制代码
初级工程师 2# iTouch 发表于 2012-2-28 09:01:45
支持
您需要登录后才可以回帖 登录 | 注册

关于我们|手机版|Archiver|DEVDIV.COM ( 京ICP备07040843号 )  

GMT+8, 2012-5-19 17:09

Powered by DEVDIV.COM!

© 2010-2012 DEVDIV.COM Coummunity.

回顶部