设为首页 收藏本站
查看: 728|回复: 0

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

[复制链接]

尚未签到

发表于 2015-5-9 10:21:45 | 显示全部楼层 |阅读模式
  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购物清单
DSC0000.jpg
  清单列表
  MainPage.xaml


DSC0001.gif DSC0002.gif 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));
        }
    }
}
DSC0003.jpg
  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));
            }
        }
    }
}
DSC0004.jpg
  查看商品详细
  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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65180-1-1.html 上篇帖子: 写在微软发布Windows Phone 7 之际: Win Phone 7 会收费吗? 下篇帖子: 《101 Windows Phone 7 Apps》读书笔记-PASSWORDS & SECRETS
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表