首页 > 代码库 > WP8独立存储 总结

WP8独立存储 总结

1、设置与属性的存储2、本地文件的存储

</pre><pre name="code" class="csharp">//独立存储  键值对IsolatedStorageSettings(独立本地设置): 命名空间为:System.IO.IsolatedStorage.IsolatedStorageSettings,提供了一系列的API,用来在独立存储中存储和操作键/值对。一般使用该方式来存储App设置和用户特定设置。          IsolatedStorageSettings mysetting = IsolatedStorageSettings.ApplicationSettings;            protected override void OnNavigatedFrom(NavigationEventArgs e)//重写onnavigatefrom          {              base.OnNavigatedFrom(e);              mysetting["text"] = this.tex.Text;//textbox设置存储              mysetting["check"] = checkbox.IsChecked;//checkbox的设置存储                mysetting["radio1"] = radio1.IsChecked;//radio设置存储              mysetting["radio2"] = radio2.IsChecked;              mysetting.Save();          }          protected override void OnNavigatedTo(NavigationEventArgs e)//重写onnaviatefrom          {              base.OnNavigatedTo(e);              if(mysetting.Contains("text"))              tex.Text = mysetting["text"] as string;              if (mysetting.Contains("check"))              {                  checkbox.IsChecked = (bool)mysetting["check"];              }              if(mysetting.Contains("radio1"))              {                  radio1.IsChecked = (bool)mysetting["radio1"];              }              if (mysetting.Contains("radio2"))              {                  radio2.IsChecked = (bool)mysetting["radio2"];              }          }            //命名空间为:System.IO.IsolatedStorage.IsolatedStorageFile,可以在虚拟的独立存储中创建、使用、删除文件和目录, 使用System.IO.IsolatedStorage.IsolatedFileStream,通过文件流(file stream)可以添加或者检索文件。独立文件流可以存储从web动态加载的图片、声音和文件。          IsolatedStorageFile filestorage = IsolatedStorageFile.GetUserStoreForApplication();//实例化IsolatedStorageFile对象                    //往isolatedstorage写数据          private void xie_Click(object sender, RoutedEventArgs e)          {              //创建文件夹              // 首先判断目录是否存在,不存在的话创建目录              if (!filestorage.DirectoryExists("myFolder"))              {                  filestorage.CreateDirectory("myFolder");              }              //创建文件并写入数据              using (var isofilestream = new IsolatedStorageFileStream("myFolder/storage.txt", FileMode.OpenOrCreate, filestorage))              {                  using (var filewriter = new StreamWriter(isofilestream))                  {                      filewriter.WriteLine(this.tex.Text);                  }              }          }            ////从Isolated Storage中读取数据          private void du_Click(object sender, RoutedEventArgs e)          {              try              {                  using (var isofilestream = new IsolatedStorageFileStream("myFolder/storage.txt", FileMode.Open, filestorage))                  {                      using (var filereader = new StreamReader(isofilestream))                      {                          this.textbolck.Text = filereader.ReadLine();//文件                      }                  }              }              catch {                  this.textbolck.Text = "读取出错,请先创建文件夹与文件";              }          }      }  

 

WP8独立存储 总结