zoukankan      html  css  js  c++  java
  • 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
    <phone:PhoneApplicationPage
    x:Class="ShoppingList_Demo.MainPage"
    xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone
    ="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell
    ="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily
    ="{StaticResource PhoneFontFamilyNormal}"
    FontSize
    ="{StaticResource PhoneFontSizeNormal}"
    Foreground
    ="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations
    ="Portrait" Orientation="Portrait"
    mc:Ignorable
    ="d" d:DesignWidth="480" d:DesignHeight="696"
    shell:SystemTray.IsVisible
    ="True">


    <phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
    <shell:ApplicationBar.MenuItems>
    <shell:ApplicationBarMenuItem Text="新增" Click="New_Click"/>
    </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>


    <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
    <TextBlock x:Name="PageTitle" Text="购物清单" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentGrid" Grid.Row="1">
    <ListBox Grid.Row="0" Margin="10" FontSize="48" Name="Files">
    </ListBox>
    </Grid>
    </Grid>


    </phone:PhoneApplicationPage>
    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
    <phone:PhoneApplicationPage
    x:Class="ShoppingList_Demo.AddItem"
    xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone
    ="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell
    ="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily
    ="{StaticResource PhoneFontFamilyNormal}"
    FontSize
    ="{StaticResource PhoneFontSizeNormal}"
    Foreground
    ="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations
    ="Portrait" Orientation="Portrait"
    mc:Ignorable
    ="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible
    ="True">

    <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
    <TextBlock x:Name="PageTitle" Text="添加商品" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentGrid" Grid.Row="1">
    <Grid.RowDefinitions>
    <RowDefinition Height="90" MinHeight="72.5"/>
    <RowDefinition Height="90" MinHeight="72.5"/>
    <RowDefinition Height="90" MinHeight="72.5"/>
    <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="100*" />
    <ColumnDefinition Width="346*"/>
    </Grid.ColumnDefinitions>

    <TextBlock Grid.Column="0" Grid.Row="0" Text="名称:" HorizontalAlignment="Center" VerticalAlignment="Center" />
    <TextBox Name="nameTxt" Grid.Column="1" Margin="8,8,8,0" Padding="2" />

    <TextBlock Grid.Column="0" Grid.Row="1" Text="价格:" HorizontalAlignment="Center" VerticalAlignment="Center" />
    <TextBox x:Name="priceTxt" Grid.Column="1" Margin="8,8,8,0" Padding="2" Grid.Row="1" />

    <TextBlock Grid.Column="0" Grid.Row="2" Text="数量:" HorizontalAlignment="Center" VerticalAlignment="Center" />
    <TextBox Name="quanTxt" Grid.Column="1" Margin="8,8,8,375" Padding="2" Grid.Row="2" Grid.RowSpan="2" />

    </Grid>
    <Button x:Name="BtnSave" Content="保存" HorizontalAlignment="Right" Margin="0,0,17,0" Grid.Row="1" VerticalAlignment="Bottom" Click="BtnSave_Click" />


    </Grid>

    </phone:PhoneApplicationPage>
    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
    <phone:PhoneApplicationPage
    x:Class="ShoppingList_Demo.DisplayPage"
    xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone
    ="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell
    ="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily
    ="{StaticResource PhoneFontFamilyNormal}"
    FontSize
    ="{StaticResource PhoneFontSizeNormal}"
    Foreground
    ="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations
    ="Portrait" Orientation="Portrait"
    mc:Ignorable
    ="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible
    ="True">

    <!--LayoutRoot contains the root grid where all other page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
    <TextBlock x:Name="PageTitle" Text="商品信息" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentGrid" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" Margin="10,10,10,77" ShowGridLines="True" Width="446" d:LayoutOverrides="GridBox">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto" MinHeight="72.5"/>
    <RowDefinition Height="Auto" MinHeight="72.5"/>
    <RowDefinition Height="Auto" MinHeight="72.5"/>
    <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="0.3*" />
    <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <TextBlock Grid.Column="0" Grid.Row="0" Text="名称:" HorizontalAlignment="Center" VerticalAlignment="Center" />
    <TextBlock Name="nameTxt" Grid.Column="1" Margin="8" Padding="2" Height="59" />

    <TextBlock Grid.Row="1" Text="价格:" HorizontalAlignment="Center" VerticalAlignment="Center" />
    <TextBlock x:Name="priceTxt" Grid.Column="1" Margin="8" Padding="2" Height="59" Grid.Row="1" />

    <TextBlock Grid.Column="0" Grid.Row="2" Text="数量:" HorizontalAlignment="Center" VerticalAlignment="Center" />
    <TextBlock Name="quanTxt" Grid.Column="1" Margin="8" Padding="2" Height="59" Grid.Row="2" />

    </Grid>
    <Button x:Name="BtnBack" Content="回到购物清单" HorizontalAlignment="Right" Margin="0,0,17,0" Grid.Row="1" VerticalAlignment="Bottom" Click="BtnBack_Click" />

    </Grid>

    </phone:PhoneApplicationPage>
    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));
    }
    }
    }
  • 相关阅读:
    居敬持志
    测试内容
    TestMarkDown
    git
    面试题
    兼容的可视区高度和宽度
    JS(数据类型、预解析、闭包、作用域、this)
    JavaScript new 一个构造函数
    Windows下gm打水印老是报gm convert: Unable to read font (n019003l.pfb)问题
    如何开始一个vue+webpack项目
  • 原文地址:https://www.cnblogs.com/linzheng/p/2002697.html
Copyright © 2011-2022 走看看