为什么使用MSDN说明无法使用app.config设置? [英] Why are app.config settings not available using MSDN instructions?

查看:92
本文介绍了为什么使用MSDN说明无法使用app.config设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我一直在尝试按照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 the System.Windows.Application class[^] has a property called Properties[^], which is hiding your application's Properties namespace. Any code within the Application class which tries to access Properties 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屋!

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