[英]Read Properties.Settings.Default from a different project
In my WPF MVVM
application, I have Model
, View
, and ViewModel
as a separate projects. 在我的
WPF MVVM
应用程序中,我将Model
, View
和ViewModel
作为单独的项目。 Now, in my View
application I have user settings defined in Settings.settings
file, I can access them through: 现在,在
View
应用程序中,我在Settings.settings
文件中定义了用户设置,可以通过以下方式访问它们:
Properties.Settings.Default.MySettingName
and in my Tool.exe.xml I can confirm that the setting is there: 在我的Tool.exe.xml中,我可以确认设置是否存在:
<userSettings>
<Tool.Properties.Settings>
<setting name="MySettingName" serializeAs="String">
<value>False</value>
</setting>
</Tool.Properties.Settings>
</userSettings>
Now the question is how do I access those settings from my ViewModel project? 现在的问题是如何从ViewModel项目访问这些设置? It's obvious that if I were to do
Properties.Settings.Default.*
it will point to it's own settings. 显然,如果我要进行
Properties.Settings.Default.*
,它将指向它自己的设置。
So far I tried to do the following two ways: 到目前为止,我尝试了以下两种方法:
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = configFile.AppSettings.Settings;
and 和
var keys = ConfigurationManager.AppSettings.AllKeys;
But both of them always return 0 entities. 但是他们两个总是返回0个实体。
I also know that I can create a ViewModel
and bind settings from the View, but for this particular case, creating a new ViewModel
just to access setings sounds a little bit overkill. 我也知道我可以创建
ViewModel
并从View绑定设置,但是对于这种特殊情况,创建一个仅用于访问设置的新ViewModel
听起来有点过分。
Thanks for help! 感谢帮助!
Thanks to the https://stackoverflow.com/a/632161/1729349 感谢https://stackoverflow.com/a/632161/1729349
Here's how I was able to access the value of the setting: 这是我访问设置值的方式:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// find section
ClientSettingsSection configSection = config.SectionGroups[@"userSettings"].Sections["Tool.Properties.Settings"] as ClientSettingsSection;
var setting = configSection.Settings.Get("MySettingName").Value.ValueXml.InnerText;
AppSettings is for the 'default' appSettings configuration section. AppSettings适用于“默认” appSettings配置部分。 You need to open your own configuration section.
您需要打开自己的配置部分。 Try this:
尝试这个:
ConfigurationManager.GetSection("userSettings");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.