MinecraftWolf
MinecraftWolf

Reputation: 39

How to store settings in UWP Application?

I have a UWP project in Visual Studio. I want to keep my settings. Example: Text wrapping is turned on by default. When the user changes this value, my app will keep this value and my app will load in my settings when it's loaded.

Upvotes: 0

Views: 106

Answers (1)

FrozenAssassine
FrozenAssassine

Reputation: 1450

In UWP this is very simple:

It stores the data object at the position given by val.

Functions:

public static void SaveSettings(string val, object data)
{
    ApplicationData.Current.LocalSettings.Values[val] = data.ToString();
}

public static string GetSettings(string val, string defaultValue = "")
{
    return ApplicationData.Current.LocalSettings.Values[val] as string ?? defaultValue;
}

Usage:

//Save data false for the value "TextWrapping":
SaveSettings("TextWrapping", false);

//Retrieve the data from "TextWrapping" and convert it back to bool
bool TextWrapping = bool.Parse(GetSettings("TextWrapping"));

//Usage of the defaultValue:
//When no data has ever been saved to the value "TextWrapping" it is null. So the function returns the provided defaultValue:
bool TextWrapping = bool.Parse(GetSettings("TextWrapping"), "false");

Here the official documentation

Upvotes: 1

Related Questions