本文共 7180 字,大约阅读时间需要 23 分钟。
在前面的几篇博客中,都是关于数据的,这方面的内容其实还有很多很多,省略掉一部分后,也还是有很多。这一篇将是很重要的一部分,关于保存和读取数据,对于游戏而言,这一点尤其重要。
先来看看一个大概的背景吧,我这里写的很简单啦^_^
保存的内容就是这四个框框里填写的数据咯。先上XAML代码。
先来看看单个设置呗,下面就是代码咯。
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;public MainPage(){ this.InitializeComponent();}private void btnSaveAppData_Click(object sender, RoutedEventArgs e){ localSettings.Values["RectangleRedHeight"] = tBoxRedHeight.Text; localSettings.Values["RectangleRedWidth"] = tBoxRedWidth.Text; localSettings.Values["RectangleGreenHeight"] = tBoxGreenHeight.Text; localSettings.Values["RectangleGreenWidth"] = tBoxGreenWidth.Text;}private void btnReadAppData_Click(object sender, RoutedEventArgs e){ Object objRectangleRedHeight = localSettings.Values["RectangleRedHeight"]; Object objRectangleRedWidth = localSettings.Values["RectangleRedWidth"]; Object objRectangleGreenHeight = localSettings.Values["RectangleGreenHeight"]; Object objRectangleGreenWidth = localSettings.Values["RectangleGreenWidth"]; recRed.Height = double.Parse(objRectangleRedHeight.ToString()); recRed.Width = double.Parse(objRectangleRedWidth.ToString()); recGreen.Height = double.Parse(objRectangleGreenHeight.ToString()); recGreen.Width = double.Parse(objRectangleGreenWidth.ToString()); }
首先定义了两个全局变量,如果看过前面几篇文章,这个应该就非常清楚了。顾名思义,第一个是用来保存本地设置的,第二个则是用来访问本地文件夹的。这里是单个设置地进行保存的,后面还有2种方式。那么就来调试吧,注意在点击了保存数据按钮之后把App关掉哦,关掉之后再加载,这样才算是保存了应用数据嘛,你说对不对呢?
以下就是我的测试结果了。
我们的设计都不用变,后台代码修改如下。
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; public MainPage() { this.InitializeComponent(); } private void btnSaveAppData_Click(object sender, RoutedEventArgs e) { Windows.Storage.ApplicationDataCompositeValue compositeSettings = new ApplicationDataCompositeValue(); compositeSettings["RectangleRedHeight"] = tBoxRedHeight.Text; compositeSettings["RectangleRedWidth"] = tBoxRedWidth.Text; compositeSettings["RectangleGreenHeight"] = tBoxGreenHeight.Text; compositeSettings["RectangleGreenWidth"] = tBoxGreenWidth.Text; localSettings.Values["RectangleSettings"] = compositeSettings; } private async void btnReadAppData_Click(object sender, RoutedEventArgs e) { Windows.Storage.ApplicationDataCompositeValue compositeSettings = (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["RectangleSettings"]; if (compositeSettings == null) { Windows.UI.Popups.MessageDialog messageDialog = new Windows.UI.Popups.MessageDialog("你好像没有保存任何应用数据哦!"); await messageDialog.ShowAsync(); } else { recRed.Height = double.Parse(compositeSettings["RectangleRedHeight"].ToString()); recRed.Width = double.Parse(compositeSettings["RectangleRedWidth"].ToString()); recGreen.Height = double.Parse(compositeSettings["RectangleGreenHeight"].ToString()); recGreen.Width = double.Parse(compositeSettings["RectangleGreenWidth"].ToString()); } }
使用ApplicationDataCompositeValue 会创建一个复合设置,通过代码所示方式即可添加数据,最后会将其添加到localSettings中。
读取数据的时候,同样是先在localSettings中通过键值对的方式来取出这个复合设置。如果该设置为空,就会调用MessageDialog控件弹窗通知没有保存数据。对于这个控件,可以访问这里:。如果复合设置存在则将他们分别进行类型转换后复制给相应的矩形的属性。
在容器存放数据其实也就这么回事啦,无非就是先创建一个容器,然后如果创建成功了,就在其中添加相应的数据即可。
至于加载数据,在这里我使用了一个bool变量来检查容器是不是已经创建好了,如果创建好了就可以将相应的数据取出然后赋值了,如果没有的话则一样挑出弹窗。
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; public MainPage() { this.InitializeComponent(); } private void btnSaveAppData_Click(object sender, RoutedEventArgs e) { Windows.Storage.ApplicationDataContainer containerSettings = localSettings.CreateContainer("RecSettingsContainer", Windows.Storage.ApplicationDataCreateDisposition.Always); if (localSettings.Containers.ContainsKey("RecSettingsContainer")) { localSettings.Containers["RecSettingsContainer"].Values["RectangleRedHeight"] = tBoxRedHeight.Text; localSettings.Containers["RecSettingsContainer"].Values["RectangleRedWidth"] = tBoxRedWidth.Text; localSettings.Containers["RecSettingsContainer"].Values["RectangleGreenHeight"] = tBoxGreenHeight.Text; localSettings.Containers["RecSettingsContainer"].Values["RectangleGreenWidth"] = tBoxGreenWidth.Text; } } private async void btnReadAppData_Click(object sender, RoutedEventArgs e) { bool hasContainerSettings = localSettings.Containers.ContainsKey("RecSettingsContainer"); if(hasContainerSettings) { recRed.Height = double.Parse(localSettings.Containers["RecSettingsContainer"].Values["RectangleRedHeight"].ToString()); recRed.Width = double.Parse(localSettings.Containers["RecSettingsContainer"].Values["RectangleRedWidth"].ToString()); recGreen.Height = double.Parse(localSettings.Containers["RecSettingsContainer"].Values["RectangleGreenHeight"].ToString()); recGreen.Width = double.Parse(localSettings.Containers["RecSettingsContainer"].Values["RectangleGreenWidth"].ToString()); } else { Windows.UI.Popups.MessageDialog messageDialog = new Windows.UI.Popups.MessageDialog("你好像没有保存任何应用数据哦!"); await messageDialog.ShowAsync(); } }
接下来就来个运行的截图咯,还有弹框的截图^_^
1.对于单个设置和复合设置
localSettings.Values.Remove("compositeSettings");
2.对于复合数据
localSettings.DeleteContainer("containerSettings");
删除并不难,或者说,这一节都不难。有了这些,我们在做游戏的时候,就可以将用户对游戏的设置都保存下来啦。
那么这一篇博文就结束咯,感谢大家的支持!
补充一个插曲哈,刚才准备写完了就保存发表的,然后就点了右上角的“关闭”……
感谢您的访问,希望对您有所帮助。
欢迎大家关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处: