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

WP8 独立存储 总结2

wp7.1APIs

Isolated Storage Classes

•独立存储类在System.IO.IsolatedStorage命名空间中
•IsolatedStorageFile
•表示包含文件和目录的独立存储区
•IsolatedFileStream
•公开独立存储中的文件

Saving Data代码

private void saveGameToIsolatedStorage(string message){  using(IsolatedStorageFile isf =  IsolatedStorageFile.GetUserStoreForApplication())  {    using(IsolatedStorageFileStreamrawStream= isf.CreateFile("MyFile.store"))    {      StreamWriterwriter = newStreamWriter(rawStream);      writer.WriteLine(message); // save the message      writer.Close();    }   }}

Loading Data 代码

 1 private string loadString() 2 { 3     stringresult = null; 4     using(IsolatedStorageFileisf = IsolatedStorageFile.GetUserStoreForApplication()) 5     { 6         if(isf.FileExists("Myfile.store") 7       { 8         using(IsolatedStorageFileStreamrawStream = isf.OpenFile(filename, System.IO.FileMode.Open))  9              {
              StreamReaderreader = newStreamReader(rawStream);10             result = reader.ReadLine();11           reader.Close();12           }
      }
13    }14    return result;15 }

例如在 AddPade.xaml.cs 上加入以下代码

 1 namespace PhoneApp1   2 {   3     public partial class AddPage : PhoneApplicationPage   4     {   5         public AddPage()   6         {   7             InitializeComponent();   8         }   9         protected override void OnNavigatedTo(NavigationEventArgs e)  10         {  11             base.OnNavigatedTo(e);  12             if (this.State.ContainsKey("IncompleteEntry"))  13             {  14                 this.logtext.Text = this.State["IncompleteEntry"] as string;  15             }  16         }  17         protected override void OnNavigatedFrom(NavigationEventArgs e)  18         {  19             if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back &&  20                 e.NavigationMode != System.Windows.Navigation.NavigationMode.Forward)  21             {  22                 this.State["IncompleteEntry"] = this.logtext.Text;  23             }  24             base.OnNavigatedFrom(e);  25         }  26   27   28         private void ApplicationBarIconButton_Click(object sender, EventArgs e)  29         {  30             SaveEntry();  31   32   33             if (NavigationService.CanGoBack)  34             {  35                 NavigationService.GoBack();  36             }  37         }  38   39   40         private void cancel_Click(object sender, EventArgs e)  41         {  42             if (NavigationService.CanGoBack)  43             {  44                 NavigationService.GoBack();  45             }  46         }  47   48   49         private async void SaveEntry()  50         {  51             string timeshow = System.DateTime.Now + System.Environment.NewLine;  52   53   54             App thisapp = App.Current as App;//利用APP属性传递值  55   56   57             thisapp.logdata =http://www.mamicode.com/ FileStorageOperation.LoadFromIsolatedStorage();  58             thisapp.logdata = http://www.mamicode.com/thisapp.logdata + timeshow + logtext.Text + System.Environment.NewLine;  59             FileStorageOperation.SavetoIsolatedStorage(thisapp.logdata);  60   61   62         }  63     }  64 }  

在APP.XAML.CS加入属性的值

1 public string logdata2         {3             set;4             get;5         }

在显示的页面的代码加入

 1 protected override void OnNavigatedTo(NavigationEventArgs e) 2         { 3             base.OnNavigatedTo(e); 4             5  6  7             App thisapp = App.Current as App; 8  9 10             thisapp.logdata =http://www.mamicode.com/ FileStorageOperation.LoadFromIsolatedStorage();11 12 13             showblock.Text = thisapp.logdata;14         }

 

WP8 独立存储 总结2