Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to keep Settings for a WinRT App?

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!

like image 511
VasileF Avatar asked Feb 02 '13 12:02

VasileF


People also ask

Where are Windows app settings stored?

The AppData folder includes application settings, files, and data unique to the applications on your Windows PC. The folder is hidden by default in Windows File Explorer and has three hidden sub-folders: Local, LocalLow, and Roaming. You won't use this folder very often, but this is where your important files reside.

Which of the following controls are recommended for app settings?

General recommendationsA toggle switch is usually the best control for a binary setting. For settings that let users choose one item from a set of up to 5 mutually exclusive, related options, use radio buttons. Create an entry point for all app settings in your app setting's page. Keep your settings simple.

What is WinRT used for?

C++/WinRT is an entirely standard modern C++17 language projection for Windows Runtime (WinRT) APIs, implemented as a header-file-based library, and designed to provide you with first-class access to the modern Windows API.

What is Windows RT API?

(WINdows RunTime) The Windows programming interface (API) that superseded Win32 and debuted with Windows 8. WinRT allows developers to create safe "sandboxed" touchscreen applications available from the Microsoft Store.


2 Answers

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;
        }
    }
}
like image 152
Iris Classon Avatar answered Oct 20 '22 17:10

Iris Classon


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.

like image 44
ChrisF Avatar answered Oct 20 '22 17:10

ChrisF