为什么使用MSDN说明无法使用app.config设置? [英] Why are app.config settings not available using MSDN instructions?
问题描述
我一直在尝试按照MSDN说明将设置添加到我的WPF桌面应用程序中。我使用设计器添加了一个设置,这会在 \bin 项目输出目录中生成一个名为MyApp.exe.config的文件。打开这个文件,我找到了我的设置。到目前为止一切都很好。
然后我转到这个页面在运行时访问设置,解释了如何在运行时读取设置。问题是我的应用程序构造函数中有一个属性
实例,但它没有设置
成员?
我做错了什么?
亲切的祝福~Patrick
我尝试了什么:
我尝试过MSDN文档。
Hi,
I have been trying to follow the MSDN instructions for adding settings to my WPF desktop application. I have added a setting using the designer and this results in a file called MyApp.exe.config in the \bin project output directory. Opening this file, I find my setting. Everything good so far.
Then I go to this page Accessing settings at run-time, which explains how to read a setting at run-time. The problem is that I have a Properties
instance in my application constructor but it has no Settings
member?
What am I doing wrong?
Kind wishes ~ Patrick
What I have tried:
I have tried following the MSDN documentation.
推荐答案
问题是System.Windows.Application
class [ ^ ] 一个名为属性的属性
[ ^ ],wh ich正在隐藏应用程序的属性
命名空间。尝试访问属性
的Application
类中的任何代码都在查看属性,而不是命名空间。
要解决此问题,您可以使用完全限定名称来引用设置:
The issue is that theSystem.Windows.Application
class[^] has a property calledProperties
[^], which is hiding your application'sProperties
namespace. Any code within theApplication
class which tries to accessProperties
is looking at the property, not the namespace.
To work around the problem, you can either use the fully-qualified name to refer to the settings:
var aSetting = YourNamespace.Properties.Settings.Default.SomeSetting;
或者您可以为其添加别名设置
类:
or you can add an alias for the Settings
class:
namespace YourNamespace
{
using Settings = Properties.Settings;
public partial class YourApplication : Application
{
public YourApplication()
{
var aSetting = Settings.Default.SomeSetting;
}
}
}
这篇关于为什么使用MSDN说明无法使用app.config设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!