您的位置:首页 > 其它

Windows Phone 学习笔记(一) 数据存储

2014-12-21 01:34 197 查看
独立存储设置IsolatedStorageSetting

private IsolatedStorageSettings _appSettings;

public MainPage()
{
InitializeComponnent();
_appSettings=IsolatedStorageSettings.ApplicationSettings;
}

if (_appSettings.Contains(key))
{
_appSettings[key]=value;
}else{
_appSettings.Add(key,value);
}
_appSettings.Save();

//删除
_appsettings.Remove(key);

//清空
_appSettings.Clear();


独立存储文件IsolatedStorageFile

调用手机的独立存储

IsolatedStorageFile storage=IsolatedStorageFile.GetUserStoreForApplication();

创建独立存储文件流

IsolatedStorageFilStream location=new IsolatedStorageFileStream(fileName,System.IO.FileMode.Create,storage);

读写该文件流

System.IO.StreamWriter file=new System.IO.StreamWriter(location);

将XML文件保存到流file觞,_doc是用户创建的文件

_doc.Save(file);

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();//关闭手机本地存储流
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: