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

 

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

清单列表

MainPage.xaml

 

  1. View Code   
  2.  
  3. <phone:PhoneApplicationPage   
  4.     x:Class="ShoppingList_Demo.MainPage" 
  5.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  6.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  7.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  8.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  9.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  10.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  12.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  13.     Foreground="{StaticResource PhoneForegroundBrush}" 
  14.     SupportedOrientations="Portrait" Orientation="Portrait" 
  15.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696" 
  16.     shell:SystemTray.IsVisible="True"> 
  17.  
  18.  
  19.     <phone:PhoneApplicationPage.ApplicationBar> 
  20.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
  21.             <shell:ApplicationBar.MenuItems> 
  22.                 <shell:ApplicationBarMenuItem Text="新增" Click="New_Click"/> 
  23.             </shell:ApplicationBar.MenuItems> 
  24.         </shell:ApplicationBar> 
  25.     </phone:PhoneApplicationPage.ApplicationBar> 
  26.       
  27.       
  28.     <!--LayoutRoot contains the root grid where all other page content is placed--> 
  29.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  30.         <Grid.RowDefinitions> 
  31.             <RowDefinition Height="Auto"/> 
  32.             <RowDefinition Height="*"/> 
  33.         </Grid.RowDefinitions> 
  34.  
  35.         <!--TitlePanel contains the name of the application and page title--> 
  36.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12"> 
  37.             <TextBlock x:Name="PageTitle" Text="购物清单" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  38.         </StackPanel> 
  39.  
  40.         <!--ContentPanel - place additional content here--> 
  41.         <Grid x:Name="ContentGrid" Grid.Row="1"> 
  42.             <ListBox Grid.Row="0" Margin="10" FontSize="48" Name="Files"> 
  43.             </ListBox> 
  44.         </Grid> 
  45.     </Grid> 
  46.       
  47.  
  48. </phone:PhoneApplicationPage> 

 

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

 

 

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

AddItem.xaml

  1. View Code   
  2.  
  3. <phone:PhoneApplicationPage   
  4.     x:Class="ShoppingList_Demo.AddItem" 
  5.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  6.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  7.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  8.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  9.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  10.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  12.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  13.     Foreground="{StaticResource PhoneForegroundBrush}" 
  14.     SupportedOrientations="Portrait" Orientation="Portrait" 
  15.     mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" 
  16.     shell:SystemTray.IsVisible="True"> 
  17.  
  18.     <!--LayoutRoot contains the root grid where all other page content is placed--> 
  19.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  20.         <Grid.RowDefinitions> 
  21.             <RowDefinition Height="Auto"/> 
  22.             <RowDefinition Height="*"/> 
  23.         </Grid.RowDefinitions> 
  24.  
  25.         <!--TitlePanel contains the name of the application and page title--> 
  26.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12"> 
  27.             <TextBlock x:Name="PageTitle" Text="添加商品" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  28.         </StackPanel> 
  29.  
  30.         <!--ContentPanel - place additional content here--> 
  31.         <Grid x:Name="ContentGrid" Grid.Row="1"> 
  32.             <Grid.RowDefinitions> 
  33.                 <RowDefinition Height="90" MinHeight="72.5"/> 
  34.                 <RowDefinition Height="90" MinHeight="72.5"/> 
  35.                 <RowDefinition Height="90" MinHeight="72.5"/> 
  36.                 <RowDefinition /> 
  37.             </Grid.RowDefinitions> 
  38.             <Grid.ColumnDefinitions> 
  39.                 <ColumnDefinition Width="100*" /> 
  40.                 <ColumnDefinition Width="346*"/> 
  41.             </Grid.ColumnDefinitions> 
  42.  
  43.             <TextBlock Grid.Column="0" Grid.Row="0" Text="名称:" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
  44.             <TextBox Name="nameTxt"  Grid.Column="1" Margin="8,8,8,0" Padding="2" /> 
  45.  
  46.             <TextBlock Grid.Column="0" Grid.Row="1" Text="价格:"  HorizontalAlignment="Center" VerticalAlignment="Center" /> 
  47.             <TextBox x:Name="priceTxt"  Grid.Column="1" Margin="8,8,8,0" Padding="2" Grid.Row="1"  /> 
  48.  
  49.             <TextBlock Grid.Column="0" Grid.Row="2" Text="数量:" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
  50.             <TextBox Name="quanTxt" Grid.Column="1" Margin="8,8,8,375" Padding="2" Grid.Row="2" Grid.RowSpan="2" /> 
  51.  
  52.         </Grid> 
  53.         <Button x:Name="BtnSave" Content="保存" HorizontalAlignment="Right" Margin="0,0,17,0" Grid.Row="1" VerticalAlignment="Bottom" Click="BtnSave_Click" /> 
  54.  
  55.     
  56.     </Grid> 
  57.  
  58. </phone:PhoneApplicationPage> 

 

  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.  
  16. namespace ShoppingList_Demo  
  17. {  
  18.     public partial class AddItem : PhoneApplicationPage  
  19.     {  
  20.         public AddItem()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.  
  25.         private void BtnSave_Click(object sender, RoutedEventArgs e)  
  26.         {  
  27.             using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())  
  28.             {  
  29.                 XDocument _doc = new XDocument();  
  30.  
  31.                 XElement _item = new XElement(nameTxt.Text);//创建一个XML元素  
  32.                 XAttribute price = new XAttribute("price", priceTxt.Text);//创建一个XML属性  
  33.                 XAttribute quantity = new XAttribute("quantity", quanTxt.Text);  
  34.  
  35.                 _item.Add(price, quantity);//将这两个属性添加到 XML元素上  
  36.                 //用_item 新建一个XML的Linq文档   
  37.                 _doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), _item);  
  38.  
  39.                 //创建一个本地存储的文件流  
  40.                 IsolatedStorageFileStream location = new IsolatedStorageFileStream(nameTxt.Text + ".item",  
  41.                         System.IO.FileMode.Create, storage);  
  42.                 //将本地存储文件流转化为可写流  
  43.                 System.IO.StreamWriter file = new System.IO.StreamWriter(location);  
  44.                 //将XML文件 保存到流file上 即已经写入到手机本地存储文件上  
  45.                 _doc.Save(file);  
  46.  
  47.                 file.Dispose();//关闭可写流  
  48.                 location.Dispose();//关闭手机本地存储流  
  49.                 //调回清单主页  
  50.                 NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));  
  51.             }  
  52.         }  
  53.     }  

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

查看商品详细

DisplayPage.xaml

  1. View Code   
  2.  
  3. <phone:PhoneApplicationPage   
  4.     x:Class="ShoppingList_Demo.DisplayPage" 
  5.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  6.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  7.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  8.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  9.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  10.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  12.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  13.     Foreground="{StaticResource PhoneForegroundBrush}" 
  14.     SupportedOrientations="Portrait" Orientation="Portrait" 
  15.     mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" 
  16.     shell:SystemTray.IsVisible="True"> 
  17.  
  18.     <!--LayoutRoot contains the root grid where all other page content is placed--> 
  19.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  20.         <Grid.RowDefinitions> 
  21.             <RowDefinition Height="Auto"/> 
  22.             <RowDefinition Height="*"/> 
  23.         </Grid.RowDefinitions> 
  24.  
  25.         <!--TitlePanel contains the name of the application and page title--> 
  26.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12"> 
  27.             <TextBlock x:Name="PageTitle" Text="商品信息" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  28.         </StackPanel> 
  29.  
  30.         <!--ContentPanel - place additional content here--> 
  31.         <Grid x:Name="ContentGrid" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" Margin="10,10,10,77" ShowGridLines="True" Width="446" d:LayoutOverrides="GridBox"> 
  32.             <Grid.RowDefinitions> 
  33.                 <RowDefinition Height="Auto" MinHeight="72.5"/> 
  34.                 <RowDefinition Height="Auto" MinHeight="72.5"/> 
  35.                 <RowDefinition Height="Auto" MinHeight="72.5"/> 
  36.                 <RowDefinition /> 
  37.             </Grid.RowDefinitions> 
  38.             <Grid.ColumnDefinitions> 
  39.                 <ColumnDefinition Width="0.3*" /> 
  40.                 <ColumnDefinition Width="*"/> 
  41.             </Grid.ColumnDefinitions> 
  42.  
  43.             <TextBlock Grid.Column="0" Grid.Row="0" Text="名称:" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
  44.             <TextBlock Name="nameTxt"  Grid.Column="1" Margin="8" Padding="2" Height="59"  /> 
  45.  
  46.             <TextBlock Grid.Row="1" Text="价格:" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
  47.             <TextBlock x:Name="priceTxt"  Grid.Column="1" Margin="8" Padding="2" Height="59" Grid.Row="1"  /> 
  48.  
  49.             <TextBlock Grid.Column="0" Grid.Row="2" Text="数量:" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
  50.             <TextBlock Name="quanTxt" Grid.Column="1" Margin="8" Padding="2" Height="59" Grid.Row="2" /> 
  51.  
  52.         </Grid> 
  53.         <Button x:Name="BtnBack" Content="回到购物清单" HorizontalAlignment="Right" Margin="0,0,17,0" Grid.Row="1" VerticalAlignment="Bottom" Click="BtnBack_Click" /> 
  54.  
  55.     </Grid> 
  56.  
  57. </phone:PhoneApplicationPage> 

 

  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.  
  17. namespace ShoppingList_Demo  
  18. {  
  19.     public partial class DisplayPage : PhoneApplicationPage  
  20.     {  
  21.           
  22.         public DisplayPage()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.  
  27.         protected override void OnNavigatedTo(NavigationEventArgs e)  
  28.         {  
  29.             String itemName = "";  
  30.  
  31.             base.OnNavigatedTo(e);  
  32.             //获取上一页面传递过来的item值  
  33.             bool itemExists = NavigationContext.QueryString.TryGetValue("item", out itemName);  
  34.  
  35.             if (itemExists)  
  36.             {  
  37.                 PageTitle.Text = itemName;  
  38.             }  
  39.  
  40.             using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())  
  41.             {  
  42.                 XElement _xml;//定义Linq的XML元素  
  43.                 //打开本地存储文件  
  44.                 IsolatedStorageFileStream location = new IsolatedStorageFileStream(itemName, System.IO.FileMode.Open, storage);  
  45.                 //转化为可读流  
  46.                 System.IO.StreamReader file = new System.IO.StreamReader(location);  
  47.                 //解析流 转化为XML  
  48.                 _xml = XElement.Parse(file.ReadToEnd());  
  49.  
  50.                 if (_xml.Name.LocalName != null)  
  51.                 {  
  52.                     XAttribute priceTemp = _xml.Attribute("price");//获取价格  
  53.                     priceTxt.Text = priceTemp.Value.ToLower();  
  54.                     XAttribute quanTemp = _xml.Attribute("quantity");//获取数量  
  55.                     quanTxt.Text = quanTemp.Value.ToLower();  
  56.                     nameTxt.Text = itemName;  
  57.                 }  
  58.  
  59.                 file.Dispose();  
  60.                 location.Dispose();  
  61.             }  
  62.         }  
  63.  
  64.         private void BtnBack_Click(object sender, RoutedEventArgs e)  
  65.         {  
  66.             NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));  
  67.         }  
  68.     }