保留WinRT应用程序设置的最佳方法? [英] Best Way to keep Settings for a WinRT App?
问题描述
我正在开发实际上也是一个游戏的WinRT应用程序.我需要以某种文件或某种方式将其他信息(例如音频设置或播放器统计信息)保留在某个位置.如果是文件,只需在或...中写入设置?我有一个主意,但我认为这太基本了...最好的方法是什么?
I'm working on a WinRT app that's actually also a game. I need to keep different information such as audio settings or player statistics somewhere in sort of a file or somehow. If it's a file, just write settings in or... ? I have an idea but I think is way too rudimentary... What is the best approach to obtain this?
任何帮助或建议,我们将不胜感激!
Any help or suggestions are greatly appreciated!
推荐答案
以下是将数据保存在WinRT应用程序中的一些方法,名称中带有Settings的方法可能就是您想要的!-刚刚添加了其他方法同样,如果需要,您还可以序列化数据.这是有效的代码-但不要忘记添加错误处理等.这是一个简单的演示代码:)
Here are some ways to save Data in a WinRT app, the method with Settings in the name is probably what you are looking for!- just added the other ones as well,- you also can serialize data if you want to. This is working code- but don't forget to add error handling etc. It's a simple demo code :)
对于设置,您可以将简单的设置保存为键和值,对于更复杂的设置,可以使用容器.我在这里提供了两个示例=)
As for settings, you can save simple settings as key and values, and for more complex settings you can use a container. I've provided both examples here =)
public class StorageExamples
{
public async Task<string> ReadTextFileAsync(string path)
{
var folder = ApplicationData.Current.LocalFolder;
var file = await folder.GetFileAsync(path);
return await FileIO.ReadTextAsync(file);
}
public async void WriteTotextFileAsync(string fileName, string contents)
{
var folder = ApplicationData.Current.LocalFolder;
var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, contents);
}
public void SaveSettings(string key, string contents)
{
ApplicationData.Current.LocalSettings.Values[key] = contents;
}
public string LoadSettings(string key)
{
var settings = ApplicationData.Current.LocalSettings;
return settings.Values[key].ToString();
}
public void SaveSettingsInContainer(string user, string key, string contents)
{
var localSetting = ApplicationData.Current.LocalSettings;
localSetting.CreateContainer(user, ApplicationDataCreateDisposition.Always);
if (localSetting.Containers.ContainsKey(user))
{
localSetting.Containers[user].Values[key] = contents;
}
}
}
这篇关于保留WinRT应用程序设置的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!