码迷,mamicode.com
首页 > 其他好文 > 详细

WP8 独立存储 总结2

时间:2014-09-16 12:14:00      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   文件   div   

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 = FileStorageOperation.LoadFromIsolatedStorage();  
58             thisapp.logdata = thisapp.logdata + timeshow + logtext.Text + System.Environment.NewLine;  
59             FileStorageOperation.SavetoIsolatedStorage(thisapp.logdata);  
60   
61   
62         }  
63     }  
64 }  

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

1 public string logdata
2         {
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 = FileStorageOperation.LoadFromIsolatedStorage();
11 
12 
13             showblock.Text = thisapp.logdata;
14         }

 

WP8 独立存储 总结2

标签:style   blog   color   io   os   ar   for   文件   div   

原文地址:http://www.cnblogs.com/NEIL-X/p/3974434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!