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 = 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         }
时间: 2024-12-09 10:19:29

WP8 独立存储 总结2的相关文章

WP8独立存储 总结

1.设置与属性的存储2.本地文件的存储 </pre><pre name="code" class="csharp">//独立存储 键值对IsolatedStorageSettings(独立本地设置): 命名空间为:System.IO.IsolatedStorage.IsolatedStorageSettings,提供了一系列的API,用来在独立存储中存储和操作键/值对.一般使用该方式来存储App设置和用户特定设置. IsolatedStora

WP8 独立存储 总结3(应用设置)

•可在独立存储中使用ApplicationSettings对象•在独立存储中存储键/值对的Dictionary方式存储 •存储的对象将永久保存 在应用设置中保存数据 1 void saveString(string message, string name) 2 { 3 IsolatedStorageSettings.ApplicationSettings[name] = message; 4 IsolatedStorageSettings.ApplicationSettings.Save();

Silverlight 独立存储(IsolatedStorageFile)

1.在Web中添加天气服务引用地址 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 2.在Web中添加Wcf服务接口IWeatherService.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.Serialization; 5 using System.Servic

Silverlight独立存储

写 1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); 4 using (Stream stream=isf.CreateFile("Weather.txt")) 5 { 6 using (StreamWriter writer=new Stream

WP开发图片保存到独立存储并从独立存储中读取

需要添加引用命名空间 using System.IO; using System.IO.IsolatedStorage; 1.将图片保存到独立存储空间 1 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 2 3 { 4 5 if (myIsolatedStorage.FileExists(App.fileName)) 6 7 { 8 9 myIsola

Windows Phone 7 中拷贝文件到独立存储

private void CopyToIsolatedStorage(){    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())    {        string[] files = new string[] { "下雨天.mp3", "用心听.mp3", "我们的歌.mp3" }; foreach (var _fil

wp8通过WebClient从服务器下载文件

通过WebClient从Web服务器下载文件,并保存到wp8手机应用程序的独立存储. 我们可以通过利用webClient_DownloadStringCompleted来获得下载完成所需要的时间,用Stopwatch得到下载的总时间. 通常我们都将上传.下载作为异步事件来处理,以便不阻止主线程. String url = "http://172.18.144.248:8080/upload/" + filename; WebClient client = new WebClient()

wp8手机浏览器项目

项目需求如下: 1.页面布局 最上方为搜索/网址框 中间为网页显示区,默认主页为百度搜索 最下方为功能栏,分别有后退,前进,窗口和更多功能 在更多功能中有 分享给好友 发送网址到桌面 查看历史记录等 2.搜索/网址框 用户在最上方的搜索/网址框中可以进行搜索或者输入网址 如果输入的网址则跳转到该网址 如果是文字内容,则对该文字进行百度搜索 用户点击输入时弹出url软键盘 回车进行搜索或者网址跳转 跳转之后软键盘关闭 在用户浏览网页时,搜索/网址框显示该网页的标题 当用户点击搜索/网址框进行操作时

[深入浅出WP8.1(Runtime)]Windows Phone 8.1和Silverlight 8.1的区别

1.2.2 Windows Phone 8.1应用程序模型 Windows Phone 8.1支持多种开发语言来开发应用程序,包括C#.VB.JavaScript和C++,那么本书的代码主要是采用C#语言来开发,部分章节采用C++.从Windows  Phone 8.1开始,如果是开发普通的应用程序可以选择的应用程序模型有:C#/XAML.VB/XAML.C++/XAML和JavaScript /HTML5.游戏开发还是采用C++的DirectX的框架.在Windows Phone 8之前如果是