【问题标题】:How to store setting with UWP C#?如何使用 UWP C# 存储设置?
【发布时间】:2017-01-10 15:10:21
【问题描述】:

我正在通过 Visual Studio 远程机器在带有 Windows 10 IoT 操作系统的 Rasbperry Pi 3 上运行 uwp 应用程序,并且选择了发布。 问题是如何保存我所做的设置并在以后运行相同的 UWP 应用程序时使用相同的设置。

它是如何完成的?我可以从文本文件中读取设置 Windows.ApplicationModel.Package.Current.InstalledLocation; 但是我当然不能将任何更改保存到同一个文本文件中。 如果我不想将更改保存到文本文件,我应该将文本文件放在哪里?

但也许我可以用这个

Windows.Storage.ApplicationData.Current.LocalSettings;

但是如何检查是否没有存储值并改为使用默认值?

我正在尝试保存这些设置,但它不起作用。 这样做的重点是,当我再次运行相同的 uwp 应用程序时,我可以使用这些相同的设置。

var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["setting1"] = textBlockDelayValue.Text;
localSettings.Values["setting2"] = checkShow;

【问题讨论】:

标签: c# uwp windows-iot-core-10


【解决方案1】:

如果我不想保存更改,我应该将文本文件放在哪里 文本文件?

您可以将文本文件放在解决方案的Windows.Storage.ApplicationData.Current.LocalFolder 中,并使用这段代码访问它:

    Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    async void WriteFile()
    {
        StorageFile sampleFile = await localFolder.CreateFileAsync("test.txt",
            CreationCollisionOption.ReplaceExisting);
        await FileIO.WriteTextAsync(sampleFile, "111");
    }

    async void ReadFile()
    {
        try
        {
            StorageFile sampleFile = await localFolder.GetFileAsync("test.txt");
            String content = await FileIO.ReadTextAsync(sampleFile);
        }
        catch (Exception)
        {
            // Timestamp not found
        }
    }

但是我如何检查是否没有存储值并放置一个 默认值?

你可以这样读出它们:

        var value1 = ApplicationData.Current.LocalSettings.Values["setting1"];
        var value2 = ApplicationData.Current.LocalSettings.Values["setting2"];

【讨论】:

    猜你喜欢
    • 2019-05-30
    • 2017-08-02
    • 2021-04-05
    • 2016-07-31
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    相关资源
    最近更新 更多