如何在 UWP 应用程序中存储设置?

问题描述 投票:0回答:1

我在 Visual Studio 中有一个 UWP 项目。我想保留我的设置。示例:文本环绕在默认情况下处于打开状态。当用户更改此值时,我的应用程序将保留此值,并且我的应用程序将在加载时加载到我的设置中。

c# uwp
1个回答
0
投票

在 UWP 中这很简单:

它将数据对象存储在val给定的位置。

功能:

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;
}

用法:

//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");

这里官方文档

© www.soinside.com 2019 - 2024. All rights reserved.