Windows Phone开发之独立存储的两种使用方法小结
来源:互联网 发布:php config 的位置 编辑:程序博客网 时间:2024/06/16 18:19
Windows Phone开发独立存储两种使用方法总结如下:
以前有个错误的理解,因为一直用模拟器开发小案例,模拟器重启之后独立存储的数据都会被删除,
就错误的以为真机可能也会出现这样的问题。
其实,不是这样的。真相是:
模拟器每次启动都会重新初始化,当然不会保存。但在真实手机上会永永保存,就像硬盘,但一旦恢复出厂设置或者刷机,也会丢失。
另外,由于WP每个应用程序都分配专用空间,所以,如果程序从手机上卸载,隔离存储中的数据也会丢失。
所以说,在WP手机上原来保存的数据存储在独立存储区域还是很不错的选择。
方法一:IsolatedStorageSetting
//保存数据IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings; string mykey = "myValue"; string myvalues = ""; if (setting.Contains(mykey)) { setting[mykey] = myTextBox.Text; } else { setting.Add(mykey, myvalues); } setting.Save();//读取数据IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings; if (setting.Contains("myValue")) { myTextBlock.Text = setting["myValue"].ToString(); }//删除数据myTextBox.Text = ""; IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings; string mykey = "myValue"; if (iso.Contains(mykey)){ iso.Remove(mykey); }
方法二:IsolatedStorageFile
//保存数据IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication(); string fileName = "myFile.txt"; using (var file = new IsolatedStorageFileStream(fileName,FileMode.OpenOrCreate,isolatedStorageFile)) { using (var writer = new StreamWriter(file)) { writer.WriteLine(myTextBox.Text); } }//读取数据IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication(); string fileName = "myFile.txt"; if(isolatedStorageFile.FileExists(fileName)){ using (var file =new IsolatedStorageFileStream(fileName,FileMode.Open,isolatedStorageFile)) { using (var reader = new StreamReader(file)) { myTextBlock.Text = reader.ReadLine(); } } }//删除数据myTextBox.Text = "";IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication(); if (isolatedStorageFile.FileExists("myFile.txt")) { isolatedStorageFile.DeleteFile("myFile.txt"); }
补充常用方法如下:
IsolatedStorage: WeatherInfo temp = null;WeatherInfo w = null;IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings; if (iss.TryGetValue("WeatherInfo", out temp))//取值先判断值是否存在 { w = temp;}代码说明:iss.TryGetValue("WeatherInfo", out temp)返回Boolean类型,temp为输出,“WeatherInfo”为Key值。
- Windows Phone开发之独立存储的两种使用方法小结
- Windows Phone开发之独立存储二
- Windows Phone开发之独立存储、文件的读写、ListBox绑定数据、NavigationService实现XAML跳转
- Windows Phone 7 开发 独立存储
- Windows Phone 独立存储之IsolatedStorageFile
- Windows Phone 独立存储之IsolatedStorageSettings
- Windows Phone 独立存储
- windows phone:独立存储
- Windows Phone 7独立存储
- 独立存储, windows phone IsolatedStorageFile
- Windows Phone 7 开发——独立存储
- Windows Phone 7独立存储之本地数据库
- Windows phone 7中的独立存储
- Windows Phone 7 开发 31 日谈——第15日:独立存储
- Windows Phone 7 开发 31 日谈——第15日:独立存储
- windows phone 8 使用独立存储保存用户的登陆信息
- windows phone 独立存储空间的操作
- Windows Phone开发之UserControl的用法
- 如何进行每页数据的汇总
- 【转】Linux内核makefile解析
- 发布适合服务端C++程序的高效日志库
- 怎么读书?
- mysql数据库的存储过程这段代码如何保存,被vb调用
- Windows Phone开发之独立存储的两种使用方法小结
- LVS、Nginx负载分衡构建实战,以及应用性能对比
- java中修改字体大小,颜色
- linux irq抽象
- Android 中颜色对应的值
- 结合使用 volatile 和 synchronized 实现 “开销较低的读-写锁”
- lua笔记之表达式
- ubuntu 11.10 /etc/resolv.conf文件重启被清除,dns设置
- C++中cout和cerr的区别?