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

31天學(xué)會Windows Phone 7開發(fā):獨立存儲

移動開發(fā)
本文是《Windows Phone 7開發(fā)31日談》系列的第十五篇文章。上一篇,我們討論了程序中的墓碑機制從而讓程序看起來是可以在后臺運行的。本文,我們來談?wù)勗陔娫捴写鎯Ρ镜財?shù)據(jù)的一種非常棒的方法。使用獨立存儲。

本文是《Windows Phone 7開發(fā)31日談》系列的第十五篇文章。上一篇,我們討論了程序中的墓碑機制從而讓程序看起來是可以在后臺運行的。本文,我們來談?wù)勗陔娫捴写鎯Ρ镜財?shù)據(jù)的一種非常棒的方法。使用獨立存儲。

什么是獨立存儲?

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

設(shè)置和文件

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

 

IsolatedStorageSettings

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

  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.     }  

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

1.如果還沒在IsolatedStorageSettings中創(chuàng)建就讀取它的值會拋出一個異常。確認(rèn)你已經(jīng)初始化了設(shè)置,或者總是使用Contains方法先檢查一下。

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

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

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

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

IsolatedStorageFile

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

  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.     }  

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

你都知道了。現(xiàn)在我們在Windows Phone 7中有兩種存儲機制可以用。IsolatedStorageSettings和IsolatedStorageFile。我很樂意聽到你在程序中使用這兩種存儲結(jié)構(gòu)的創(chuà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/

【編輯推薦】

  1. 31天學(xué)會Windows Phone 7開發(fā):墓碑機制(多任務(wù))
  2. 31天學(xué)會Windows Phone 7開發(fā):位置服務(wù)
  3. 31天學(xué)會Windows Phone 7開發(fā):使手機震動
  4. 31天學(xué)會Windows Phone 7開發(fā):加速感應(yīng)器
  5. 31天學(xué)會Windows Phone 7開發(fā):輸入范圍和文本框
責(zé)任編輯:王曉東 來源: 博客園
相關(guān)推薦

2012-06-13 13:01:57

Windows Pho

2012-08-02 10:16:39

Windows Pho

2012-06-06 13:48:34

Windows Pho

2012-06-11 13:08:10

Windows Pho

2012-08-09 13:39:22

Windows Pho

2012-08-16 11:31:30

Windows Pho

2012-06-12 10:43:20

Windows Pho

2012-08-13 09:56:45

Windows Pho

2012-06-25 16:14:26

Windows Pho

2012-06-07 09:33:13

Windows Pho

2012-06-19 09:31:53

Windows Pho

2012-06-20 10:21:50

Windows Pho

2012-07-13 14:41:12

2012-07-24 10:15:34

Windows Pho

2012-06-21 10:59:31

Windows Pho

2012-07-11 09:21:35

Windows Pho

2012-07-31 09:44:27

Windows Pho

2012-06-29 14:13:10

2010-12-01 09:01:31

獨立存儲Windows Pho

2013-04-19 16:52:24

Windows PhoWindows Pho
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 欧美一二区 | 美女黄视频网站 | 久久毛片| av中文字幕在线观看 | 国产高清一区二区三区 | 婷婷丁香在线视频 | 福利网址 | 国产在线视频在线观看 | 久久精品国产亚洲 | 欧美日韩视频一区二区 | 美女视频黄的免费 | 欧美精品成人 | 亚洲成av片人久久久 | 久久精品中文 | 久久久久国产精品午夜一区 | a级毛片毛片免费观看久潮喷 | 中文字幕一区在线观看视频 | 色久电影 | www.99热这里只有精品 | www..com18午夜观看 | aaaa日韩| 久久精品91久久久久久再现 | 国产成人免费视频网站高清观看视频 | 久久影音先锋 | 成人免费视频在线观看 | 色婷婷精品 | 精品伊人 | 91精品国产91久久久久久最新 | 亚洲午夜小视频 | 亚洲 精品 综合 精品 自拍 | 中文字字幕一区二区三区四区五区 | www.日韩 | 国产精品久久久久久久久久免费看 | 国产精品国产馆在线真实露脸 | 中文在线a在线 | 99在线免费观看视频 | 久久人 | 91在线视频免费观看 | 密桃av| 亚洲区一区二 | 国产区精品 |