31天學(xué)會(huì)Windows Phone 7開發(fā):獨(dú)立存儲(chǔ)
本文是《Windows Phone 7開發(fā)31日談》系列的第十五篇文章。上一篇,我們討論了程序中的墓碑機(jī)制從而讓程序看起來是可以在后臺(tái)運(yùn)行的。本文,我們來談?wù)勗陔娫捴写鎯?chǔ)本地?cái)?shù)據(jù)的一種非常棒的方法。使用獨(dú)立存儲(chǔ)。
什么是獨(dú)立存儲(chǔ)?
獨(dú)立存儲(chǔ)不是一個(gè)新概念。在Silverlight 2中已經(jīng)在使用了。本質(zhì)上說這是一種在本地文件系統(tǒng)中存儲(chǔ)數(shù)據(jù)或文件的方式。“獨(dú)立(isolated)”是因?yàn)橹挥心愕某绦虿趴梢栽L問這些數(shù)據(jù)。如果你有兩個(gè)應(yīng)用程序,同時(shí)你想在它們之間共享數(shù)據(jù)的話,***使用一些類似基于云的可以讓你共享數(shù)據(jù)的服務(wù)。一個(gè)應(yīng)用程序不能共享,調(diào)用設(shè)備上其他的應(yīng)用程序或與之進(jìn)行交互。
設(shè)置和文件
有兩種方式在本地存儲(chǔ)你的數(shù)據(jù)。***是通過庫(kù)中的鍵/值對(duì),叫做IsolatedStorageSettings。第二是通過創(chuàng)建真實(shí)的文件和目錄,叫做IsolatedStorageFile。下圖簡(jiǎn)要介紹了這些(由MSDN提供),我會(huì)為每種方式提供一個(gè)深入的例子。
IsolatedStorageSettings
有很多時(shí)候,這可能是你需要的唯一存儲(chǔ)方式。IsolatedStorageSettings允許你在一個(gè)字典中存儲(chǔ)鍵/值對(duì)(注意,無需任何設(shè)定),然后再讀取出來。這些數(shù)據(jù)會(huì)一直保存著,無論應(yīng)用程序停止/啟動(dòng),或者關(guān)機(jī)等等。除非你刪除它,或者用戶卸載你的應(yīng)用程序,否則它一直存在。要記住的一點(diǎn)是在它被添加到字典中之前你無法讀取它。在我的每個(gè)例子中,你都會(huì)看到在讀取數(shù)據(jù)之前檢查值是否它存在的代碼。下面的例子是在用戶在你的程序中接收電子郵件更新時(shí)需要保存用戶設(shè)定的代碼。我用了一個(gè)多選框允許用戶選擇,還有一個(gè)將此值保存到獨(dú)立存儲(chǔ)中的事件。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using Microsoft.Phone.Controls;
- using System.IO.IsolatedStorage;
- namespace Day15_IsolatedStorage
- {
- public partial class MainPage : PhoneApplicationPage
- {
- IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- InitializeSettings();
- }
- private void InitializeSettings()
- {
- if (settings.Contains("emailFlag"))
- {
- EmailFlag.IsChecked = (bool)settings["emailFlag"];
- }
- else settings.Add("emailFlag", false);
- }
- private void EmailFlag_Unchecked(object sender, RoutedEventArgs e)
- {
- settings["emailFlag"] = false;
- }
- private void EmailFlag_Checked(object sender, RoutedEventArgs e)
- {
- settings["emailFlag"] = true;
- }
- }
- }
正如你所見,這非常簡(jiǎn)單。請(qǐng)記住以下內(nèi)容:
1.如果還沒在IsolatedStorageSettings中創(chuàng)建就讀取它的值會(huì)拋出一個(gè)異常。確認(rèn)你已經(jīng)初始化了設(shè)置,或者總是使用Contains方法先檢查一下。
2.你可以在設(shè)置中保存任意內(nèi)容。在我的例子中,我保存了一個(gè)布爾值,但你可以保存一個(gè)客戶對(duì)象,或者任何你能想到的。
3.記住當(dāng)你讀取數(shù)據(jù)時(shí)你需要將它顯示強(qiáng)制轉(zhuǎn)換。你會(huì)看到我在使用之前將數(shù)據(jù)轉(zhuǎn)換為bool值。雖然你保存了對(duì)象,但并沒有保存它的類型。是否能看到類型取決于你自己。
4.設(shè)置一個(gè)值和在庫(kù)中添加它效果是一樣。“settings.Add()”的語句實(shí)際上不是必需的,我添加它是為了讓你看清語法。
就這些。IsolatedStorageSettings非常簡(jiǎn)單。只用極少的代碼就可保存鍵/值對(duì)。創(chuàng)建和保存文件相對(duì)略復(fù)雜一些,但還是十分簡(jiǎn)單。
IsolatedStorageFile
使用IsolatedStorageFile是一種讓你可以在用戶的設(shè)備中存儲(chǔ)真實(shí)文件的機(jī)制。在我的例子中,在一個(gè)子目錄中創(chuàng)建了一個(gè)文本文件,并讀取文件中的內(nèi)容。我們還可以創(chuàng)建和刪除目錄,子目錄及文件。看起來有很多代碼,但實(shí)際上非常簡(jiǎn)單。我們創(chuàng)建一個(gè)新的IsolatedStorageFile對(duì)象,并使用一個(gè)IsolatedStorageFileStream對(duì)象將它寫入到驅(qū)動(dòng)器中。我在代碼中加入了注釋,這樣你可以更清楚地看到發(fā)生了什么。有兩個(gè)事件處理程序,一個(gè)用來保存文件,另一個(gè)讀?。?/p>
- using System.IO.IsolatedStorage;
- using System.IO;
- private void SaveButton_Click(object sender, RoutedEventArgs e)
- {
- //Obtain a virtual store for application
- IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
- //Create new subdirectory
- fileStorage.CreateDirectory("textFiles");
- //Create a new StreamWriter, to write the file to the specified location.
- StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.OpenOrCreate, fileStorage));
- //Write the contents of our TextBox to the file.
- fileWriter.WriteLine(writeText.Text);
- //Close the StreamWriter.
- fileWriter.Close();
- }
- private void GetButton_Click(object sender, RoutedEventArgs e)
- {
- //Obtain a virtual store for application
- IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
- //Create a new StreamReader
- StreamReader fileReader = null;
- try
- {
- //Read the file from the specified location.
- fileReader = new StreamReader(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.Open, fileStorage));
- //Read the contents of the file (the only line we created).
- string textFile = fileReader.ReadLine();
- //Write the contents of the file to the TextBlock on the page.
- viewText.Text = textFile;
- fileReader.Close();
- }
- catch
- {
- //If they click the view button first, we need to handle the fact that the file hasn't been created yet.
- viewText.Text = "Need to create directory and the file first.";
- }
- }
離開程序時(shí)這多像一個(gè)迷人的魔術(shù),再回來時(shí),會(huì)再次載入文件(它還在那兒!)。
你都知道了?,F(xiàn)在我們?cè)赪indows Phone 7中有兩種存儲(chǔ)機(jī)制可以用。IsolatedStorageSettings和IsolatedStorageFile。我很樂意聽到你在程序中使用這兩種存儲(chǔ)結(jié)構(gòu)的創(chuàng)新用法。請(qǐng)留言!
下載代碼示例
這個(gè)例子將上面展示的代碼融合到了一個(gè)項(xiàng)目中。
原作者:Jeff Blankenburg 譯者:金山崟霸
中文來源:http://www.cnblogs.com/porscheyin/archive/2010/12/23/1914528.html
英文來源:http://www.jeffblankenburg.com/2010/10/15/31-days-of-windows-phone-day-15-isolated-storage/
【編輯推薦】