成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用獨立存儲開發Windows Phone 7應用程序

移動開發
前不久我們講過".NET平臺開發Windows Phone 7、iPhone及Android應用",在新的更新包內微軟給Windows Phone 7增加了一項獨立存儲的概念,就是在移動智能設備中存儲本地數據的方法。

前不久我們講過".NET平臺開發Windows Phone 7、iPhone及Android應用",在新的更新包內微軟給Windows Phone 7增加了一項獨立存儲的概念,就是在移動智能設備中存儲本地數據的方法。

什么是獨立存儲?

獨立存儲不是一個新概念。在Silverlight 2中已經在使用了。本質上說這是一種在本地文件系統中存儲數據或文件的方式。“獨立(isolated)”是因為只有你的程序才可以訪問這些數據。如果你有兩個應用程序,同時你想在它們之間共享數據的話,***使用一些類似基于云的可以讓你共享數據的服務。一個應用程序不能共享,調用設備上其他的應用程序或與之進行交互。

  1.     void SaveLocal(string data)    
  2.     {    
  3. #if (MonoTouch || MonoDroid)    
  4.         File.WriteAllText(_localPath, data);    
  5. #elif WINDOWS_PHONE    
  6.         using (var appStorage =     
  7.             IsolatedStorageFile.GetUserStoreForApplication())    
  8.         {    
  9.             var file = appStorage.OpenFile(_localPath, FileMode.Create);    
  10.             FileExtension.WriteAllText(file, data);    
  11.         }    
  12. #endif    
  13.     }    
  14.  

設置和文件

有兩種方式在本地存儲你的數據。***是通過庫中的鍵/值對,叫做IsolatedStorageSettings。第二是通過創建真實的文件和目錄,叫做IsolatedStorageFile。下圖簡要介紹了這些(由MSDN提供),我會為每種方式提供一個深入的例子。

使用獨立存儲開發Windows Phone 7應用程序
IsolatedStorageSettings

有很多時候,這可能是你需要的唯一存儲方式。IsolatedStorageSettings允許你在一個字典中存儲鍵/值對(注意,無需任何設定),然后再讀取出來。這些數據會一直保存著,無論應用程序停止/啟動,或者關機等等。除非你刪除它,或者用戶卸載你的應用程序,否則它一直存在。要記住的一點是在它被添加到字典中之前你無法讀取它。在我的每個例子中,你都會看到在讀取數據之前檢查值是否它存在的代碼。下面的例子是在用戶在你的程序中接收電子郵件更新時需要保存用戶設定的代碼。我用了一個多選框允許用戶選擇,還有一個將此值保存到獨立存儲中的事件。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using System.IO.IsolatedStorage;  
  14.  
  15. namespace Day15_IsolatedStorage  
  16. {  
  17.     public partial class MainPage : PhoneApplicationPage  
  18.     {  
  19.         IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;  
  20.           
  21.         // Constructor  
  22.         public MainPage()  
  23.         {  
  24.             InitializeComponent();  
  25.             InitializeSettings();  
  26.         }  
  27.  
  28.         private void InitializeSettings()  
  29.         {  
  30.             if (settings.Contains("emailFlag"))  
  31.             {  
  32.                 EmailFlag.IsChecked = (bool)settings["emailFlag"];  
  33.             }  
  34.             else settings.Add("emailFlag", false);  
  35.         }  
  36.  
  37.         private void EmailFlag_Unchecked(object sender, RoutedEventArgs e)  
  38.         {  
  39.             settings["emailFlag"] = false;  
  40.         }  
  41.  
  42.         private void EmailFlag_Checked(object sender, RoutedEventArgs e)  
  43.         {  
  44.             settings["emailFlag"] = true;  
  45.         }  
  46.     }  
  47. }  
  48.  

正如你所見,這非常簡單。請記住以下內容:

如果還沒在IsolatedStorageSettings中創建就讀取它的值會拋出一個異常。確認你已經初始化了設置,或者總是使用Contains方法先檢查一下。

你可以在設置中保存任意內容。在我的例子中,我保存了一個布爾值,但你可以保存一個客戶對象,或者任何你能想到的。

記住當你讀取數據時你需要將它顯示強制轉換。你會看到我在使用之前將數據轉換為bool值。雖然你保存了對象,但并沒有保存它的類型。是否能看到類型取決于你自己。

設置一個值和在庫中添加它效果是一樣。“settings.Add()”的語句實際上不是必需的,我添加它是為了讓你看清語法。

就這些。IsolatedStorageSettings非常簡單。只用極少的代碼就可保存鍵/值對。創建和保存文件相對略復雜一些,但還是十分簡單。

IsolatedStorageFile

使用IsolatedStorageFile是一種讓你可以在用戶的設備中存儲真實文件的機制。在我的例子中,在一個子目錄中創建了一個文本文件,并讀取文件中的內容。我們還可以創建和刪除目錄,子目錄及文件。看起來有很多代碼,但實際上非常簡單。我們創建一個新的IsolatedStorageFile對象,并使用一個IsolatedStorageFileStream對象將它寫入到驅動器中。我在代碼中加入了注釋,這樣你可以更清楚地看到發生了什么。有兩個事件處理程序,一個用來保存文件,另一個讀取:

  1. using System.IO.IsolatedStorage;  
  2. using System.IO;  
  3.  
  4. private void SaveButton_Click(object sender, RoutedEventArgs e)  
  5. {  
  6.     //Obtain a virtual store for application  
  7.     IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();  
  8.  
  9.     //Create new subdirectory  
  10.     fileStorage.CreateDirectory("textFiles");  
  11.  
  12.     //Create a new StreamWriter, to write the file to the specified location.  
  13.     StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.OpenOrCreate, fileStorage));  
  14.     //Write the contents of our TextBox to the file.  
  15.     fileWriter.WriteLine(writeText.Text);  
  16.     //Close the StreamWriter.  
  17.     fileWriter.Close();  
  18. }  
  19.  
  20. private void GetButton_Click(object sender, RoutedEventArgs e)  
  21. {  
  22.     //Obtain a virtual store for application  
  23.     IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();  
  24.     //Create a new StreamReader  
  25.     StreamReader fileReader = null;  
  26.  
  27.     try  
  28.     {  
  29.         //Read the file from the specified location.  
  30.         fileReader = new StreamReader(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.Open, fileStorage));  
  31.         //Read the contents of the file (the only line we created).  
  32.         string textFile = fileReader.ReadLine();  
  33.  
  34.         //Write the contents of the file to the TextBlock on the page.  
  35.         viewText.Text = textFile;  
  36.         fileReader.Close();  
  37.     }  
  38.     catch  
  39.     {  
  40.         //If they click the view button first, we need to handle the fact that the file hasn't been created yet.  
  41.         viewText.Text = "Need to create directory and the file first.";  
  42.     }  
  43. }  
  44.  

離開程序時這多像一個迷人的魔術,再回來時,會再次載入文件(它還在那兒!)。

你都知道了。現在我們在Windows Phone 7中有兩種存儲機制可以用。IsolatedStorageSettings和IsolatedStorageFile。我們非常希意聽到你在程序中使用這兩種存儲結構的創新用法,與我們一同分享。

【編輯推薦】

  1. .NET平臺開發Windows Phone 7、iPhone及Android應用
  2. 簡述Windows Phone 7應用程序開發平臺
  3. Windows Phone 7對比Android 平分秋色
  4. 多圖詳解 Windows Phone 7功能升級過程
  5. Windows Phone 7開發工具發布更新包 附下載地址
責任編輯:佚名 來源: ITpub
相關推薦

2011-03-21 09:05:40

IronRubyWindows Pho

2010-11-03 15:10:04

SilverlightSilverlightWindows Pho

2012-08-01 10:26:33

Windows Pho

2011-04-08 10:02:06

日歷Windows Pho

2012-05-17 14:15:10

Windows Pho

2011-04-01 13:20:40

Windows Pho應用程序

2013-07-30 13:38:27

Windows PhoWindows Pho

2013-07-31 14:50:32

Windows PhoWP應用程序生命周期

2011-10-25 10:24:03

Windows Pho

2012-05-28 15:37:20

WP程序生命周期

2013-07-30 11:18:37

Windows PhoWindows Pho

2010-08-27 09:36:57

Windows Pho

2013-04-19 15:35:54

Windows Pho隔離存儲

2012-08-16 10:35:50

Windows Pho

2011-06-07 11:35:38

Windows Pho

2010-12-14 18:48:49

微軟

2011-12-03 21:03:14

Windows Pho

2011-06-08 10:01:36

Windows Pho 應用程序

2011-06-08 10:24:38

Windows Pho 應用程序

2011-02-22 10:23:43

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 伊人一区 | 欧美在线视频网 | 日韩免费三级 | 中文字幕黄色大片 | 午夜久久久久久久久久一区二区 | 成人精品国产免费网站 | 久久99蜜桃综合影院免费观看 | a级大片| 一二区电影 | 欧美一区两区 | 女朋友的闺蜜3韩国三级 | 狠狠的操 | 黄色大片视频 | 国产乱码精品一区二区三区中文 | 精品国产黄a∨片高清在线 www.一级片 国产欧美日韩综合精品一区二区 | 久久a久久 | 天天想天天干 | 久久久精品久久 | 久久精品亚洲欧美日韩久久 | 亚洲视频一区二区三区 | 久国久产久精永久网页 | 日韩欧美一区二区三区免费观看 | 青青草av在线播放 | av永久免费 | 天天躁日日躁狠狠躁白人 | 久久久国产精品视频 | 欧美1—12sexvideos | 国产成人精品a视频一区www | 国产欧美在线一区二区 | 日韩欧美国产一区二区三区 | 色伊人网 | 妞干网福利视频 | 人人澡人人爱 | 欧美高清视频在线观看 | 精品久久香蕉国产线看观看亚洲 | 亚洲国产精品一区二区久久 | 国产在线一区二区三区 | 瑞克和莫蒂第五季在线观看 | 99在线国产 | 一区二区av | 成人在线免费观看视频 |