问题描述
使用.Net Core> = 3.0持久保存WPF应用程序的用户设置的首选方法是什么?
创建了WPF .Net Core 3.0项目(VS2019 V16.3.1)现在,我已经看到不再有Properties.Settings部分.
在线搜索之后,开始深入研究Microsoft.Extensions.Configuration.
除了the肿的代码可以访问设置外,现在更糟糕的是->无法保存吗?
在启动时读取变量 var culture = new CultureInfo(Properties.Settings.Default.LanguageSettings);
当变量更改时->立即保存 Properties.Settings.Default.LanguageSettings = selected.TwoLetterISOLanguageName;Properties.Settings.Default.Save();
您可以添加相同的旧的良好设置文件,例如通过右键单击属性"->添加"->新项目",然后搜索设置".可以在设置设计器中编辑该文件,然后在.net Framework项目中使用该文件(ConfigurationManager,Settings.Default.Upgrade(),Settings.Default.Save等起作用).
还将app.config文件添加到项目根文件夹(通过添加->新建项的相同方法),再次保存设置,编译项目,您将在输出中找到一个.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 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
What is the prefered (and easy/fast/simple) way for persisting user settings for WPF applications with .Net Core >=3.0?
Before <= .Net 4.8
it was as easy as:
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 dotnet核心中的UserSettings/ApplicationSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!