溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

WP7 應(yīng)用數(shù)據(jù)存儲IsolatedStorage 篇

發(fā)布時間:2020-06-19 01:58:16 來源:網(wǎng)絡(luò) 閱讀:383 作者:gnie 欄目:開發(fā)技術(shù)

     Windows Phone 7 在獨立存儲(Isolated Storage)功能方面提供了兩種數(shù)據(jù)存儲方法:文件存儲(IsolatedStorageFile)、鍵/值存儲(IsolatedStorageSettings)。通過獨立存儲我們能夠?qū)?yīng)用程序數(shù)據(jù)進(jìn)行保存,例如:用戶設(shè)置、程序運行狀態(tài)等。本篇主要講解IsolatedStorageSettings 使用方法。

     IsolatedStorageSettings 實際上是提供了一個Dictionary<TKey, TValue> 泛型類,通過鍵值Tkey 與數(shù)值TValue 的映射將應(yīng)用程序的數(shù)據(jù)存儲起來。首先在程序中通過IsolatedStorageSettings 類創(chuàng)建一個全局settings,同時再定義一個整型變量以便后續(xù)測試。

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;  int testInt = 10;

添加兩個按鍵:一個用來為testInt 執(zhí)行“+1”操作,另一個用來顯示當(dāng)前testInt 的值。

private void addBtn_Click(object sender, RoutedEventArgs e)  {      testInt++;  }    private void showBtn_Click(object sender, RoutedEventArgs e)  {      MessageBox.Show(testInt.ToString());  }

     接下來再添加一個Save 按鍵,用于保存testInt 數(shù)值和TextBox 數(shù)值。先使用Contains(string key) 方法檢查當(dāng)前是否存在“textbox”鍵值,如果沒有則使用Add(string key, object value) 方法添加該鍵值,鍵對應(yīng)的數(shù)值類型可按開發(fā)需要自行定義,本例中則使用了String 和Int 兩種類型。

private void saveBtn_Click(object sender, RoutedEventArgs e)  {      if (!settings.Contains("textbox"))      {          settings.Add("textbox", textBox.Text);      }      else      {          settings["textbox"] = textBox.Text;      }        if (!settings.Contains("integer"))      {          settings.Add("integer", testInt);      }      else      {          settings["integer"] = testInt;      }      settings.Save();  }

     當(dāng)每次重新啟動程序時,可以直接從settings 中獲取相應(yīng)數(shù)據(jù)。為保險起見使用TryGetValue<T>(string key, out T value) 獲取指定鍵的值,若鍵值不存在會返回False。

public MainPage()  {      InitializeComponent();        string textVal;      if (settings.TryGetValue<string>("textbox", out textVal))      {          textBox.Text = textVal;      }      else      {          textBox.Text = "No Text";      }        int intVal;      if (settings.TryGetValue<int>("integer", out intVal))      {          testInt = intVal;      }      else      {          testInt = 10;      }  }

附上XAML 代碼:

<TextBox x:Name="textBox" Width="460" Height="72" />  <Button x:Name="addBtn" Content="Add" Width="160" Height="72"           Click="addBtn_Click"/>  <Button x:Name="showBtn" Content="Show" Width="160" Height="72"          Click="showBtn_Click"/>  <Button x:Name="saveBtn" Content="Save" Width="160" Height="72"          Click="saveBtn_Click"/>  

測試運行

如果不進(jìn)行保存,每次啟動TextBox 都會顯示“No Text”,testInt 數(shù)值為10。

WP7 應(yīng)用數(shù)據(jù)存儲IsolatedStorage 篇 WP7 應(yīng)用數(shù)據(jù)存儲IsolatedStorage 篇

點擊“Save”后,退出程序重新進(jìn)入,修改的內(nèi)容便會保存下來。

WP7 應(yīng)用數(shù)據(jù)存儲IsolatedStorage 篇 WP7 應(yīng)用數(shù)據(jù)存儲IsolatedStorage 篇

相關(guān)資料

System.IO.IsolatedStorage

IsolatedStorageFile

IsolatedStorageSettings

IsolatedStorageSettings.TryGetValue<T>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI