保留 WinRT 应用程序设置的最佳方式? [英] Best Way to keep Settings for a WinRT App?

查看:31
本文介绍了保留 WinRT 应用程序设置的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个实际上也是游戏的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆