日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

Windows8 Metro开发 (04) : 保存/读取本地应用程序设置

發(fā)布時(shí)間:2025/6/15 windows 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows8 Metro开发 (04) : 保存/读取本地应用程序设置 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
有些時(shí)候我們需要保存應(yīng)用程序的設(shè)置,如用戶的系統(tǒng)設(shè)定。在Android中,我們可以使用sharepreference。在Metro中我們該怎么做呢?
保存/讀取基本類型數(shù)據(jù)
Metro程序會把要保存的數(shù)據(jù)寫入ApplicationData.Current.LocalSettings字典中,并保存在本地。程序在開始運(yùn)行的時(shí)候會從本地初始化該字典。加載之前保存的數(shù)據(jù)。這樣我們就可以方便的保存/讀取基本類型數(shù)據(jù)了。 我將其封裝成了一個(gè)工具類。 [csharp]?view plaincopyprint?
  • using?System;??
  • using?System.Collections.Generic;??
  • using?System.Linq;??
  • using?System.Text;??
  • using?System.Threading.Tasks;??
  • using?Windows.Storage;??
  • ??
  • namespace?Win8_Study.Pages??
  • {??
  • ????class?LocalDataUtil??
  • ????{??
  • ????????#region?保存/讀取基本類型數(shù)據(jù)??
  • ????????public?static?void?SaveData(string?key,?object?value)??
  • ????????{??
  • ????????????ApplicationData.Current.LocalSettings.Values[key]?=?value;??
  • ????????}??
  • ??
  • ????????public?static?object?GetData(string?key)??
  • ????????{??
  • ????????????return?ApplicationData.Current.LocalSettings.Values[key];??
  • ????????}??
  • ??
  • ??
  • ????????public?static?void?RemoveData(string?key)??
  • ????????{??
  • ????????????ApplicationData.Current.LocalSettings.Values.Remove(key);??
  • ????????}??
  • ????????#endregion??
  • ????}??
  • }??

  • 下面我們來看一個(gè)示例:
    默認(rèn)顯示的字體大小為24,我們將其字體改為28后返回到主頁面,然后重新進(jìn)入該頁面。你會發(fā)現(xiàn)字體的大小變?yōu)?8了。 重新啟動程序進(jìn)入該頁面,你會發(fā)現(xiàn)字體的大小仍然為28。 下拉列表的選中項(xiàng)與字體大小是時(shí)刻對應(yīng)的.
    實(shí)現(xiàn)方法如下1.每次進(jìn)入該頁面的時(shí)候,首先獲取之前保存的字體大小 a.沒有獲取到 將字體大小設(shè)為默認(rèn)值 b.獲取到 將字體大小設(shè)為獲取的值 2.用戶改變字體大小時(shí),保存改變后的值 [csharp]?view plaincopyprint?
  • ????public?sealed?partial?class?LocalDataPage?:?Win8_Study.Common.LayoutAwarePage??
  • ????{??
  • ????????private?readonly?string?TEXT_VALUE?=?"國米_百度百科\n"?+??
  • ????????????"國際米蘭足球俱樂部(Football?Club?Internazionale?Milano,簡稱?Inter?或?Internazionale)"?+??
  • ????????????"是一家位于意大利北部倫巴第區(qū)米蘭市的足球俱樂部。";??
  • ????????private?readonly?double?TEXT_FONT_SIZE?=?24;??
  • ????????private?readonly?string?TEXT_FONT_SIZE_KEY?=?"LocalDataPage-TEXT_FONT_SIZE_KEY";??
  • ??
  • ????????public?LocalDataPage()??
  • ????????{??
  • ????????????this.InitializeComponent();??
  • ????????}??
  • ??
  • ????????protected?override?void?LoadState(Object?navigationParameter,?Dictionary<String,?Object>?pageState)??
  • ????????{??
  • ????????????Init();??
  • ????????}??
  • ??
  • ??
  • ????????private?void?Init()??
  • ????????{??
  • ????????????//首先讀取之前保存的設(shè)置,如果為空設(shè)置成默認(rèn)狀態(tài)??
  • ????????????InitFontSize();??
  • ????????????leftTextBlock.Text?=?TEXT_VALUE;??
  • ????????}??
  • ??
  • ????????protected?override?void?SaveState(Dictionary<String,?Object>?pageState)??
  • ????????{??
  • ????????}??
  • ??
  • ????????#region?保存程序設(shè)置??
  • ????????private?void?OnLeftComboBoxSelectionChanged(object?sender,?SelectionChangedEventArgs?e)??
  • ????????{??
  • ????????????var?comboBox?=?sender?as?ComboBox;??
  • ????????????var?item?=?comboBox.SelectedItem?as?ComboBoxItem;??
  • ????????????SetTextFontSize(Convert.ToDouble(item.Content));??
  • ????????}??
  • ??
  • ????????private?void?SetTextFontSize(double?size)??
  • ????????{??
  • ????????????leftTextBlock.FontSize?=?size;??
  • ????????????LocalDataUtil.SaveData(TEXT_FONT_SIZE_KEY,?size);??
  • ????????}??
  • ??
  • ????????private?void?InitFontSize()??
  • ????????{??
  • ????????????var?obj?=?LocalDataUtil.GetData(TEXT_FONT_SIZE_KEY);??
  • ????????????double?size?=?TEXT_FONT_SIZE;??
  • ????????????if?(obj?!=?null)??
  • ????????????{??
  • ????????????????size?=?Convert.ToDouble(obj);??
  • ????????????}??????
  • ????????????foreach?(var?element?in?leftFontSizeComboBox.Items)??
  • ????????????{??
  • ????????????????var?item?=?element?as?ComboBoxItem;??
  • ????????????????if?(item.Content.ToString().Equals(size.ToString()))??
  • ????????????????{??
  • ????????????????????leftFontSizeComboBox.SelectedItem?=?item;??
  • ????????????????????break;??
  • ????????????????}??
  • ????????????}??????????????
  • ????????}??
  • ????????#endregion??
  • ????????...??
  • ????}??
  • 或許你會嘗試著用這種方法去非基本類型的數(shù)據(jù)(比如Page,Color什么的)。嘿嘿,掛了吧。 那么我們該怎樣去保存非基本類型的數(shù)據(jù)呢?比如一個(gè)包含學(xué)生信息的集合?

    保存/讀取非基本類型的數(shù)據(jù)--序列化/反序列化 保存程序的實(shí)時(shí)數(shù)據(jù)是十分必要的。比如你從網(wǎng)絡(luò)上獲取了一些娛樂新聞并顯示給用戶,你需要將這些數(shù)據(jù)保存下來,以便程序下次運(yùn)行的時(shí)候使用。 下次運(yùn)行程序的時(shí)候,這些數(shù)據(jù)就會變成本地的了,加載速度會非常快,因?yàn)槟悴恍枰偃木W(wǎng)絡(luò)獲取數(shù)據(jù)。 如果你不這樣做的話,用戶可能會在網(wǎng)絡(luò)非常擁塞的情況下看到一個(gè)非常"干凈"的屏幕。這個(gè)時(shí)候你的應(yīng)用也許會被(應(yīng)該是必須)。。。
    舉一個(gè)"稍微"復(fù)雜點(diǎn)的例子 現(xiàn)在有Student,Coder兩個(gè)類,它們都繼承了父類People。 [csharp]?view plaincopyprint?
  • using?System;??
  • using?System.Collections.Generic;??
  • using?System.Linq;??
  • using?System.Runtime.Serialization;??
  • using?System.Text;??
  • using?System.Threading.Tasks;??
  • ??
  • namespace?Win8_Study.Pages??
  • {??
  • ????[DataContract]??
  • ????public?abstract?class?People??
  • ????{??
  • ????????[DataMember]??
  • ????????public?string?Name?{?get;?set;?}??
  • ????????[DataMember]??
  • ????????public?int?Age?{?get;?set;?}??
  • ??
  • ????????public?People(string?name,int?age)??
  • ????????{??
  • ????????????this.Name?=?name;??
  • ????????????this.Age?=?age;??
  • ????????}??
  • ????}??
  • ??
  • ????[DataContract]??
  • ????public?class?Student?:?People??
  • ????{??
  • ????????[DataMember]??
  • ????????public?int?Score?{?get;?set;?}??
  • ??
  • ????????public?Student(string?name,?int?age,?int?score)??
  • ????????????:?base(name,?age)??
  • ????????{??
  • ????????????this.Score?=?score;??
  • ????????}??
  • ????}??
  • ??
  • ????[DataContract]??
  • ????public?class?Coder?:?People??
  • ????{??
  • ????????[DataMember]??
  • ????????public?int?WorkYears?{?get;?set;?}??
  • ??
  • ????????public?Coder(string?name,?int?age,?int?workYears)??
  • ????????????:?base(name,?age)??
  • ????????{??
  • ????????????this.WorkYears?=?workYears;??
  • ????????}??
  • ????}??
  • }??
  • 我們需要在ListView上隨機(jī)顯示一些學(xué)生和程序員的信息,并保存下來。然后清空ListView,讀取保存的數(shù)據(jù),看結(jié)果與之前的是否相同。

    創(chuàng)建學(xué)生和程序員信息的方法很簡單,在這里創(chuàng)建5-10條信息,每條信息的內(nèi)容隨機(jī)顯示: [csharp]?view plaincopyprint?
  • private?List<People>?GetPeopleDatas()??
  • ????????{??
  • ????????????List<People>?peoples?=?new?List<People>();??
  • ????????????Random?ran?=?new?Random(DateTime.Now.Millisecond);??
  • ????????????int?count?=?ran.Next(5)?+?5;//5?-?10??
  • ????????????for?(int?i?=?0;?i?<?count;?++i)??
  • ????????????{??
  • ????????????????int?type?=?ran.Next(2);??
  • ????????????????if?(type?==?0)??
  • ????????????????{??
  • ????????????????????peoples.Add(new?Student("學(xué)生"?+?(i?+?1),?ran.Next(12)?+?6,?60?+?ran.Next(41)));??
  • ????????????????}??
  • ????????????????else??
  • ????????????????{??
  • ????????????????????peoples.Add(new?Coder("程序員"?+?(i?+?1),?ran.Next(10)?+?22,?ran.Next(5)));??
  • ????????????????}??
  • ????????????}??
  • ????????????return?peoples;??
  • ????????}??
  • 根據(jù)類別創(chuàng)建不同ListView項(xiàng) [csharp]?view plaincopyprint?
  • private?void?OnRightRandomAddDataButtonClicked(object?sender,?RoutedEventArgs?e)??
  • ????????{??
  • ????????????_peoples?=?GetPeopleDatas();??
  • ????????????SetListViewData(_peoples);??
  • ????????}??
  • [csharp]?view plaincopyprint?
  • private?void?SetListViewData(List<People>?peoples)??
  • {??
  • ????itemListView.Items.Clear();??
  • ????foreach?(People?p?in?peoples)??
  • ????{??
  • ????????ListViewItem?item?=?new?ListViewItem();??
  • ????????item.FontSize?=?20;??
  • ????????if?(p?is?Student)??
  • ????????{??
  • ????????????Student?s?=?p?as?Student;??
  • ????????????item.Content?=?string.Format("{0}?年齡:{1}?成績:?{2}",?s.Name,?s.Age,?s.Score);??
  • ????????}??
  • ????????else??
  • ????????{??
  • ????????????Coder?c?=?p?as?Coder;??
  • ????????????item.Content?=?string.Format("{0}?年齡:{1}?工作時(shí)間:?{2}年",?c.Name,?c.Age,?c.WorkYears);??
  • ????????}??
  • ????????itemListView.Items.Add(item);??
  • ????}??
  • }??

  • 保存數(shù)據(jù) [csharp]?view plaincopyprint?
  • ????????private?async?void?OnRightSaveDataButtonClicked(object?sender,?RoutedEventArgs?e)??
  • ????????{??
  • ????????????await?SerializerUtil.XMLSerialize(_peoples,typeof(List<People>));??
  • ????????????await?PopupUtil.ShowMessageDialog(string.Format("保存數(shù)據(jù)成功!?item數(shù)量{0}",_peoples.Count),?"提示");??
  • ????????}??
  • 注意到People,Student,Coder中的序列化標(biāo)志的嗎?不添加的話是無法序列化的。 其中?SerializerUtil.XMLSerialize是我封裝的序列化代碼的方法,其實(shí)現(xiàn)方式如下:
    [csharp]?view plaincopyprint?
  • public?static?async?Task?XMLSerialize(object?instance,?Type?type)??
  • {??
  • ????//取得當(dāng)前程序存放數(shù)據(jù)的目錄??
  • ????StorageFolder?folder?=?Windows.Storage.ApplicationData.Current.LocalFolder;??
  • ????//定義文件名??
  • ????string?fileName?=?"LocalDataPage-list_data.xml";??
  • ????//創(chuàng)建文件,如果文件存在就覆蓋??
  • ????StorageFile?newFile?=?await?folder.CreateFileAsync(fileName,?CreationCollisionOption.ReplaceExisting)??
  • ????//將內(nèi)容序列化至文件??
  • ????Stream?newFileStream?=?await?newFile.OpenStreamForWriteAsync();??
  • ????DataContractSerializer?ser?=?new?DataContractSerializer(type,?GetTypes());??
  • ????ser.WriteObject(newFileStream,?instance);??
  • ????newFileStream.Dispose();??
  • }??
  • 注意GetTypes()方法,在序列化的時(shí)候需要指定序列化對象的類型集合。在這里需要序列化的數(shù)據(jù)類型有3個(gè):People,Student,Coder。所以我們應(yīng)該這樣設(shè)定: [csharp]?view plaincopyprint?
  • private?static?ObservableCollection<Type>?GetTypes()??
  • ????????{??
  • ????????????//添加要序列化的類型??
  • ????????????if?(_Types?==?null)??
  • ????????????{??
  • ????????????????_Types?=?new?ObservableCollection<Type>();??
  • ????????????????_Types.Add(typeof(People));??
  • ????????????????_Types.Add(typeof(Student));??
  • ????????????????_Types.Add(typeof(Coder));??
  • ????????????}??
  • ????????????return?_Types;??
  • ????????}??
  • 其中_Types是全局對象ObservableCollection<Type>。 至此,數(shù)據(jù)就保存好了。
    讀取數(shù)據(jù) 讀取數(shù)據(jù)也就是進(jìn)行反序列化,我們可以得到之前保存的對象集合,其數(shù)據(jù)類型是List<People>,然后我們將該對象集合交給ListView顯示即可。 [csharp]?view plaincopyprint?
  • private?async?void?OnRightLoadDataButtonClicked(object?sender,?RoutedEventArgs?e)??
  • ????????{??
  • ????????????try??
  • ????????????{??
  • ????????????????var?obj?=?await?SerializerUtil.XMLDeserialize(typeof(List<People>));??
  • ????????????????_peoples?=?obj?as?List<People>;??
  • ????????????????SetListViewData(_peoples);??
  • ????????????????await?PopupUtil.ShowMessageDialog(string.Format("讀取數(shù)據(jù)成功!?item數(shù)量{0}",?_peoples.Count),??
  • ????????????????????"提示");??
  • ????????????????return;??
  • ????????????}??
  • ????????????catch?(FileNotFoundException)??
  • ????????????{??
  • ??????????????????
  • ????????????}??
  • ????????????await?PopupUtil.ShowMessageDialog("你還沒有保存數(shù)據(jù)。",?"提示");??
  • ????????}??
  • 反序列化 [csharp]?view plaincopyprint?
  • public?static?async?Task<object>?XMLDeserialize(Type?type)??
  • ???????{??
  • ???????????StorageFolder?folder?=?Windows.Storage.ApplicationData.Current.LocalFolder;??
  • ???????????string?fileName?=?"LocalDataPage-list_data.xml";??
  • ???????????StorageFile?newFile?=?await?folder.GetFileAsync(fileName);??
  • ???????????Stream?newFileStream?=?await?newFile.OpenStreamForReadAsync();??
  • ???????????//進(jìn)行反序列化??
  • ???????????DataContractSerializer?ser?=?new?DataContractSerializer(type,?GetTypes());??
  • ???????????object?instance?=?ser.ReadObject(newFileStream);??
  • ???????????newFileStream.Dispose();??
  • ???????????return?instance;??
  • ???????}??
  • 可以看到讀取的數(shù)據(jù)與之前保存的相同.我們的目的達(dá)到了。
    程序運(yùn)行的效果如下: 1.隨機(jī)顯示學(xué)生和程序員信息: 2.保存數(shù)據(jù) 保存文件的內(nèi)容 [html]?view plaincopyprint?
  • <ArrayOfPeople?xmlns="http://schemas.datacontract.org/2004/07/Win8_Study.Pages"?xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><People?i:type="Student"><Age>14</Age><Name>學(xué)生1</Name><Score>66</Score></People><People?i:type="Coder"><Age>24</Age><Name>程序員2</Name><WorkYears>4</WorkYears></People><People?i:type="Student"><Age>7</Age><Name>學(xué)生3</Name><Score>86</Score></People><People?i:type="Coder"><Age>23</Age><Name>程序員4</Name><WorkYears>1</WorkYears></People><People?i:type="Coder"><Age>25</Age><Name>程序員5</Name><WorkYears>2</WorkYears></People></ArrayOfPeople>??
  • 3.清除數(shù)據(jù)并讀取之前保存的數(shù)據(jù) 顯示效果同第一張圖.

    總結(jié)

    以上是生活随笔為你收集整理的Windows8 Metro开发 (04) : 保存/读取本地应用程序设置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。