相当于 WPF .NET 5、.NET 6 或 .Net Core 中的 UserSettings/ApplicationSettings [英] Equivalent to UserSettings / ApplicationSettings in WPF .NET 5, .NET 6 or .Net Core

查看:40
本文介绍了相当于 WPF .NET 5、.NET 6 或 .Net Core 中的 UserSettings/ApplicationSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 .NET 5、.NET 6 或 .Net Core >=3.0 保留 WPF 应用程序的用户设置的首选方法是什么?

创建 WPF .Net Core 3.0 项目 (VS2019 V16.3.1)现在我看到没有 Properties.Settings 部分了.

上网搜索后,开始深入微软.Extensions.Configuration.

除了访问设置的臃肿代码之外,现在更糟 ->没有保存?


  • 启动时读取变量
    var culture = new CultureInfo(Properties.Settings.Default.LanguageSettings);

  • 当变量改变时 ->立即保存
    Properties.Settings.Default.LanguageSettings = selected.TwoLetterISOLanguageName;Properties.Settings.Default.Save();

  • 解决方案

    您可以添加相同的旧设置文件,例如通过右键单击属性"->添加"->新项目"并搜索设置".该文件可以在设置设计器中进行编辑,并在之前的 .net 框架项目中使用(ConfigurationManager、Settings.Default.Upgrade()、Settings.Default.Save 等工作).

    将 app.config 文件也添加到项目根文件夹中(通过 Add -> New Item 的方式相同),再次保存设置,编译项目,您将在输出中找到一个 .dll.config 文件文件夹.您现在可以像以前一样更改默认应用值.

    使用 Visual Studio 1.16.3.5 和 .net core 3.0 WPF 项目进行测试.

    What is the prefered way for persisting user settings for WPF applications with .NET 5, .NET 6 or .Net Core >=3.0?

    Created WPF .Net Core 3.0 Project (VS2019 V16.3.1) Now I have seen there is no Properties.Settings section anymore.

    After online search, started to dive into Microsoft.Extensions.Configuration.

    Beside the bloated code to access the settings, now even worse -> No save?
    User Configuration Settings in .NET Core

    Fortunately or unfortunately the Microsoft.Extensions.Configuration does not support saving by design. Read more in this Github issue Why there is no save in ConfigurationProvider?


    What is the prefered (and easy/fast/simple) way for persisting user settings for WPF applications with .Net Core >=3.0?
    Before `

    • add the variables to the Properties.

    • Read the variables at startup
      var culture = new CultureInfo(Properties.Settings.Default.LanguageSettings);

    • when a variable changes -> immediately save it
      Properties.Settings.Default.LanguageSettings = selected.TwoLetterISOLanguageName; Properties.Settings.Default.Save();

    解决方案

    You can add the same old good settings file e.g. via the right click on the Properties -> Add -> New Item and search for the "Settings". The file can be edited in the settings designer and used as in the .net framework projects before (ConfigurationManager, Settings.Default.Upgrade(), Settings.Default.Save, etc. works).

    Add also the app.config file to the project root folder (the same way via the Add -> New Item), save the settings once again, compile the project and you will find a .dll.config file in the output folder. You can change now default app values as before.

    Tested with Visual Studio 1.16.3.5 and a .net core 3.0 WPF project.

    这篇关于相当于 WPF .NET 5、.NET 6 或 .Net Core 中的 UserSettings/ApplicationSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆