Windows Phone筆記(9)使用隔離儲存區 (Isolated Storage)(上)

來源:互聯網
上載者:User
文章目錄
  • 使用IsolatedStorageSettings類儲存應用程式設定

  在前面的筆記中我們瞭解到如何在頁面間共用資料,但是這些資料是保持在記憶體中的,當程式被終止時儲存的資料就會丟失,在很多時候我們有必要對資料進行持久化,例如,儲存程式的配置,啟動資訊等。在Windows Phone中我們可以通過隔離儲存區 (Isolated Storage)來實現資料的持久化。但是在這裡為了提高系統的安全性,Windows Phone中的應用程式中所有的I/O操作只限於使用隔離儲存區 (Isolated Storage),並且只能訪問本應用程式目錄下的隔離儲存區 (Isolated Storage)

  首先根據要儲存的資料類型,Windows Phone的隔離儲存區 (Isolated Storage)的使用類型有下面三種,我們會對下面的使用隔離儲存區 (Isolated Storage)的類型分別進行介紹,以及如何使用它。

  • 設定:使用 IsolatedStorageSettings 類將資料存放區為鍵/值對。

  • 檔案和檔案夾:使用 IsolatedStorageFile 類隱藏檔和檔案夾。

  • 關係資料:使用 LINQ to SQL 將關係資料存放區在本機資料庫中。    

使用IsolatedStorageSettings類儲存應用程式設定

  在我們的應用程式中,經常會對應用程式做一些設定,以符合應用程式的需要,或適應自己的操作習慣。比如說對應用程式更換主題、皮膚,對應用程式的啟動設定等等。要想實現這些功能我們必須對我們隊應用程式作出的更改和設定資料進行持久化,即使程式關閉了,下次也一樣可以範圍這些儲存的資料,在Windows Phone中我們使用IsolatedStorageSettings類對應用程式進行設定,該類使用索引值對的方式來儲存資料。下面我們通過一個簡單樣本來示範如何使用IsolatedStorageSetting類來儲存資料。

  該樣本的作用是儲存頁面退出前的背景色,重寫運行程式後載入還原儲存的背景色。只有一個MainPage頁面,前台頁面的代碼不需要修改,這裡就這在給出。

首先我們還是需要使用在上一篇筆記中使用過的App類,因為該類的執行個體存在於整個應用程式的生命週期中的,所有很適合用來在整個應用程式中共用資料,著這裡我們在App類中添加一個屬性,用來儲存背景色,以及添加兩個方法,分別用來儲存和載入程式的設定,需要注意的使我們的App類是一個部分類別(可以將類、結構、介面的定義拆分到兩個或多個源檔案):

 1     public partial class App : Application
2 {
3 //應用程式設定
4 public Brush MainPageBackgroundBrush { get; set; }
5
6 /// <summary>
7     /// 載入設定
8      /// </summary>
9 void LoadSettings()
10 {
11 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
12 Color mainPageClr;
13 //檢查是否儲存了背景色
14 if (settings.TryGetValue<Color>("MainPageBackgroundColor", out mainPageClr))
15 {
16 this.MainPageBackgroundBrush = new SolidColorBrush(mainPageClr);//載入儲存的設定
17 }
18 }
19
20 /// <summary>
21      /// 儲存設定
22      /// </summary>
23 void SaveSettings()
24 {
25 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
26
27 if (this.MainPageBackgroundBrush is SolidColorBrush)
28 {
29 settings["MainPageBackgroundColor"] = (this.MainPageBackgroundBrush as SolidColorBrush).Color;//儲存設定
30 }
31 settings.Save();
32
33 }
34 }

那麼現在就有一個問題了,我們該什麼時候儲存設定?什麼時候又該載入設定呢?我們可以通過利用Windows Phone中應用程式頁面的生命週期的特點來編寫相關的代碼。在Windows Phone中我們可以利用應用程式狀態管理的4個主事件(包含在Application對象中)來完成我們的操作,它們分別是:

  • Launching:當應用程式啟動時發生。

  • Deactivated:當應用程式被取消啟用時發生。

  • Activated:當之前處於休眠狀態或被邏輯刪除之後應用程式變為活動時發生。

  • Closing:當應用程式退出時發生。

Windows Phone應用程式的保留狀態所必須處理的生命週期、執行流和事件。

        十秒原則:如果儲存臨時狀態所花的時間超過 10 秒,則系統將終止應用程式。

瞭解了這四個程式狀態管理的事件後,前面的問題我們就很容易的回答了:Launching事件和Activated事件發生時,我們載入設定Deactivated事件和Closing事件發生時,我們保持設定。下面我們把儲存和載入設定方法的調用放在,Windows Phone項目產生的模板中已經添加的這些事件狀態管理函數中,下面是App.xaml.cs添加的代碼:

 1      // 應用程式啟動(例如,從“開始”菜單啟動)時執行的代碼
2      // 此代碼在重新啟用應用程式時不執行
3 private void Application_Launching(object sender, LaunchingEventArgs e)
4 {
5 LoadSettings();//載入設定
6 }
7
8 // 啟用應用程式(置於前台)時執行的代碼
9     // 此代碼在初次開機應用程式時不執行
10 private void Application_Activated(object sender, ActivatedEventArgs e)
11 {
12 LoadSettings();//載入設定
13 }
14
15 // 停用應用程式(發送到後台)時執行的代碼
16     // 此代碼在應用程式關閉時不執行
17 private void Application_Deactivated(object sender, DeactivatedEventArgs e)
18 {
19 SaveSettings();//儲存設定
20 }
21
22 // 應用程式關閉(例如,使用者點擊“後退”)時執行的代碼
23     // 此代碼在停用應用程式時不執行
24 private void Application_Closing(object sender, ClosingEventArgs e)
25 {
26 SaveSettings();//儲存設定
27 }

 

準備工作都已經做好了,那麼我們下面給出我們MainPage.xaml.cs的幕後處理代碼:

 1 public partial class MainPage : PhoneApplicationPage
2 {
3 Random ran = new Random();
4 // 建構函式
5 public MainPage()
6 {
7 InitializeComponent();
8 //為使用隔離儲存區 (Isolated Storage)設定訪問App類
9 Brush brush = (Application.Current as App).MainPageBackgroundBrush;
10
11 if (brush != null)
12 {
13 this.ContentPanel.Background = brush;
14 }
15 }
16
17 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
18 {
19 SolidColorBrush brush = new SolidColorBrush(Color.FromArgb
20 (255, (byte)ran.Next(255), (byte)ran.Next(255), (byte)ran.Next(255)));
21 this.ContentPanel.Background = brush;
22
23 //為使用隔離儲存區 (Isolated Storage)設定,儲存到App類
24 (Application.Current as App).MainPageBackgroundBrush = brush;
25
26 base.OnManipulationStarted(e);
27 }
28 }

編譯運行程式:

運行程式隨機產生一個背景色,然後點擊手機的"Back","Start"按鈕,重新回到我們的應用程式,我們可以看到背景色已經被載入為上次程式退出時保持的設定。

好,那麼這裡我們已經知道了如何通過使用IsolatedStorageSettings 類來保持應用程式的設定,在後面的筆記中我們學習如何使用 IsolatedStorageFile 類隱藏檔和檔案夾。

 

參考資料:

  http://msdn.microsoft.com/zh-cn/library/ff817008(v=vs.92).aspx(重要)

  http://msdn.microsoft.com/zh-cn/library/ff967547(v=vs.92).aspx

  http://msdn.microsoft.com/zh-cn/library/hh307995.aspx(重要)

  http://msdn.microsoft.com/zh-cn/library/ff967548(v=vs.92).aspx

  《Programming Windows Phone 7 Microsoft Silverlight Edition》

作者:晴天豬

出處:http://www.cnblogs.com/IPrograming

本文著作權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。

  Windows Phone開發人員交流群:79339880,歡迎大家來一起討論交流,共同學習進步。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.