Windows Phone开发之独立存储的两种使用方法小结

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值。



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值