2168575 发表于 2015-5-9 10:21:45

Windows Phone 7 独立存储使用XML文件来存储信息

  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购物清单

  清单列表
  MainPage.xaml


View Code





   
      
            
               
            
      
   
   
   
      
            
            
      
      
      
            
      
      
      
            
            
      
   
   




using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
namespace ShoppingList_Demo
{
    public partial class MainPage : PhoneApplicationPage
    {
      public MainPage()
      {
            InitializeComponent();
            //加载页面触发Loaded事件
            Loaded += (object sender, RoutedEventArgs e) =>
            {
                Files.Items.Clear();//先清空一下ListBox的数据
                //获取应用程序的本地存储文件
                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                  //获取并循环 *.item的存储文件
                  foreach (string filename in storage.GetFileNames("*.item"))
                  {
                        //动态构建一个Grid
                        Grid a = new Grid();
                        //定义第一列
                        ColumnDefinition col = new ColumnDefinition();
                        GridLength gl = new GridLength(200);
                        col.Width = gl;
                        a.ColumnDefinitions.Add(col);
                        //定义第二列
                        ColumnDefinition col2 = new ColumnDefinition();
                        GridLength gl2 = new GridLength(200);
                        col2.Width = gl;
                        a.ColumnDefinitions.Add(col2);
                        //添加一个TextBlock现实文件名 到第一列
                        TextBlock txbx = new TextBlock();
                        txbx.Text = filename;
                        Grid.SetColumn(txbx, 0);
                        //添加一个HyperlinkButton链接到购物详细清单页面 这是第二列
                        HyperlinkButton btn = new HyperlinkButton();
                        btn.Width = 200;
                        btn.Content = "查看详细";
                        btn.Name = filename;
                        btn.NavigateUri = new Uri("/DisplayPage.xaml?item=" + filename, UriKind.Relative);//传递文件名到商品详细页面

                        Grid.SetColumn(btn, 1);
                        a.Children.Add(txbx);
                        a.Children.Add(btn);
                        Files.Items.Add(a);
                  }
                }
            };
      }
      private void New_Click(object sender, EventArgs e)
      {
            NavigationService.Navigate(new Uri("/AddItem.xaml", UriKind.Relative));
      }
    }
}

  AddItem.xaml


View Code




   
   
      
            
            
      
      
      
            
      
      
      
            
               
               
               
               
            
            
               
               
            
            
            
            
            
            
            
      
      

   




using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
namespace ShoppingList_Demo
{
    public partial class AddItem : PhoneApplicationPage
    {
      public AddItem()
      {
            InitializeComponent();
      }
      private void BtnSave_Click(object sender, RoutedEventArgs e)
      {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                XDocument _doc = new XDocument();
                XElement _item = new XElement(nameTxt.Text);//创建一个XML元素
                XAttribute price = new XAttribute("price", priceTxt.Text);//创建一个XML属性
                XAttribute quantity = new XAttribute("quantity", quanTxt.Text);
                _item.Add(price, quantity);//将这两个属性添加到 XML元素上
                //用_item 新建一个XML的Linq文档
                _doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), _item);
                //创建一个本地存储的文件流
                IsolatedStorageFileStream location = new IsolatedStorageFileStream(nameTxt.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("/MainPage.xaml", UriKind.Relative));
            }
      }
    }
}

  查看商品详细
  DisplayPage.xaml


View Code




   
   
      
            
            
      
      
      
            
      
      
      
            
               
               
               
               
            
            
               
               
            
            
            
            
            
            
            
      
      
   




using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Windows.Navigation;
namespace ShoppingList_Demo
{
    public partial class DisplayPage : PhoneApplicationPage
    {
      public DisplayPage()
      {
            InitializeComponent();
      }
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
            String itemName = "";
            base.OnNavigatedTo(e);
            //获取上一页面传递过来的item值
            bool itemExists = NavigationContext.QueryString.TryGetValue("item", out itemName);
            if (itemExists)
            {
                PageTitle.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 priceTemp = _xml.Attribute("price");//获取价格
                  priceTxt.Text = priceTemp.Value.ToLower();
                  XAttribute quanTemp = _xml.Attribute("quantity");//获取数量
                  quanTxt.Text = quanTemp.Value.ToLower();
                  nameTxt.Text = itemName;
                }
                file.Dispose();
                location.Dispose();
            }
      }
      private void BtnBack_Click(object sender, RoutedEventArgs e)
      {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
      }
    }
}
页: [1]
查看完整版本: Windows Phone 7 独立存储使用XML文件来存储信息