Best Way to keep Settings for a WinRT App?
Asked Answered
P

2

6

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!

Plover answered 2/2, 2013 at 12:7 Comment(0)
B
17

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;
        }
    }
}
Boson answered 2/2, 2013 at 14:37 Comment(3)
Thank you very much, but my question is now : Can't I save all settings in a file somehow...? Just to read textlines from the file when I need them at app launch or to rewrite that file as text when to save? Is this a good practice?Plover
The container will give you that possibility- just update the value of the keys you want changed.Boson
And if there is a lot of data you could also use a small database such as SQLite, there is a .Net wrapper for it,- just make sure you use the async implementation. I would still recommend using the composite/container settings based on what you have told us.Boson
W
3

The MSDN has an article on using app settings in Windows Store apps.

The Windows.UI.ApplicationSettings namespace contains all the classes you need.

Provides classes that allow developers to define the app settings that appear in the settings pane of the Windows shell. The settings pane provides a consistent place for users to access app settings.

Basically these classes let you store application settings and hook them into the standard place for all application settings. Your users don't have to learn anything new, the settings will be in the expected place.

Weaver answered 2/2, 2013 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.