C# 当前上下文中不存在属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9665335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:22:05  来源:igfitidea点击:

Properties does not exist in the current context

c#wpf

提问by Cipher

I have to store some strings in the Settings file of my project. However, I am not able to access the Settings file to store the strings.

我必须在我的项目的设置文件中存储一些字符串。但是,我无法访问设置文件来存储字符串。

I definitely have accessed Properties file in an earlier project without facing any problems to store values.

我肯定在早期项目中访问过属性文件,而没有遇到任何存储值的问题。

Properties.Settings.Default.test = ...
MyApp.Properties.Settings.Default.test = ...

Properties.Settings.Default.test = ...
MyApp.Properties.Settings.Default.test = ...

But here in the current project, I am constantly being driven mad by getting the Properties does not exist in the current contextIs there any setting or something that has changed because of which Propertiesdoes not appear in Intellisense?

但通过获取这里在当前项目中,我不断地被逼疯Properties does not exist in the current context是否有已经因为其改变任何设置或东西Properties不会出现在智能感知?

采纳答案by Daniel

I had this same problem occur when I had been copying and pasting code for plug-ins. It turns out that I had accidentally left the namespace of the other project in place when I copied. By changing the namespace back to the appropriate project, I was able to use the Properties.

我在复制和粘贴插件代码时遇到了同样的问题。事实证明,我在复制时不小心将另一个项目的命名空间留在了原处。通过将命名空间改回适当的项目,我能够使用属性。

回答by Daniel Hilgarth

Make sure your project hasa settings file by going to the project properties and clicking on the Settings tab. If there is no settings file, it will tell you so and provide you with the option to create one.

通过转到项目属性并单击“设置”选项卡,确保您的项目具有设置文件。如果没有设置文件,它会告诉您并为您提供创建一个的选项。

回答by Adrien Plisson

at compile-time, Visual Studio generates a c# class which allows access to user and application settings. the generated class is defined in the default namespacespecified in the project settings.

在编译时,Visual Studio 生成允许访问用户和应用程序设置的 ac# 类。生成的类在项目设置中指定的默认命名空间中定义。

  • you may have to prefix any access to the Properties namespace with the default namespace defined in the project settings;
  • or you may change the default namespace: go to project settings, application tab, and edit the default namespace of the project.
  • 您可能必须使用项目设置中定义的默认命名空间作为对 Properties 命名空间的任何访问的前缀;
  • 或者您可以更改默认命名空间:转到项目设置,应用程序选项卡,然后编辑项目的默认命名空间。

回答by shadow_map

I just had the same problem. But the problem occured when I added a new setting to an already defined settings file (created a long time ago). Somehow adding a setting to the settings file made Visual Studio change the setting's namespace to the project default namespace(which had changed since the settings where modified last time).

我只是遇到了同样的问题。但是当我向已经定义的设置文件(很久以前创建)添加新设置时出现了问题。以某种方式向设置文件添加设置使 Visual Studio 将设置的命名空间更改为项目默认命名空间(自上次修改设置以来已更改)。

So, because the settings access modifier was set to Internal, and the class trying to access the settings still where in the old previous namespace (still in the same project though), the settings became invisible from that class.

因此,因为设置访问修饰符被设置为内部,并且尝试访问设置的类仍然在旧的以前的命名空间中(尽管仍然在同一个项目中),这些设置从该类中变得不可见。

回答by rajeemcariazo

In my case, i have referenced a resource that has an Internal/Public access modifier. I have changed its access modifier to Public and the problem was gone.

就我而言,我引用了一个具有内部/公共访问修饰符的资源。我已将其访问修饰符更改为 Public,问题就消失了。

回答by The Red Pea

In my case I was referencing one project (A) from another (B). Both in the same solution.

在我的情况下,我从另一个(B)引用了一个项目(A)。两者都在同一个解决方案中。

  • A has a public class (AClass) with a public static method. (AMethod)
  • I wanted to Test the results of this AClass.AMethod(...)
  • Visual Studio "colored" AClass in the call AClass.AMethod(...)
  • and I could right-click AClass and "Go to Definition",

    • which took me either directly to the source if I added referenced to the "Solution" project
    • Or to the DLL "outline" if I added reference to the DLL created by building Project "A"
  • But when I tried to build project "B" I got the error "AClass does not exist in current context"

  • It turns out Project B was targeting 4.5 and Project A was targeting 4.5.2
  • This answer helped: https://stackoverflow.com/a/15556365/1175496
  • A 有一个带有公共静态方法的公共类 (AClass)。(一个方法)
  • 我想测试这个 AClass.AMethod(...) 的结果
  • Visual Studio 在调用 AClass.AMethod(...) 时“着色”了 AClass
  • 我可以右键单击 AClass 并“转到定义”,

    • 如果我添加了对“解决方案”项目的引用,这将我直接带到源代码
    • 或者,如果我添加了对通过构建项目“A”创建的 DLL 的引用,则添加到 DLL“大纲”
  • 但是当我尝试构建项目“B”时,出现错误“当前上下文中不存在 AClass”

  • 结果证明,项目 B 的目标是 4.5,项目 A 的目标是 4.5.2
  • 这个答案有帮助:https: //stackoverflow.com/a/15556365/1175496

回答by Godsmith

I had this error when I accidentally had the wrong namespace as the Default namespace in the project properties.

当我不小心将错误的命名空间作为项目属性中的默认命名空间时,我遇到了这个错误。

To choose the default namespace, right click the project in the Solution ExplorerProperties->Application tab->Default namespace and check that it matches the namespace of your .cs files:

要选择默认命名空间,请右键单击解决方案资源管理器属性->应用程序选项卡->默认命名空间中的项目,并检查它是否与 .cs 文件的命名空间匹配:

[...]
using System.Text;
using System.Threading;

    namespace Correct.Namespace //<-- should match Default namespace field
    {
        static class Program
        {
            [...]

回答by CSharpLearner

Make sure you can see your setting in Settings.Designer.cs file. If not, try deleting and reentering the setting. For some reason, sometimes, the code auto generated by the tool does not include your setting. In that case, the setting wont be available in intellisense.

确保您可以在 Settings.Designer.cs 文件中看到您的设置。如果没有,请尝试删除并重新输入设置。出于某种原因,有时,该工具自动生成的代码不包括您的设置。在这种情况下,该设置在智能感知中将不可用。

回答by Serge Voloshenko

First, try to recreate your project from scratch, if it's your testing environment. This step will recreate default namespace properly. You may skip this step if you are confident with your namespace configurations.

首先,如果它是您的测试环境,请尝试从头开始重新创建您的项目。此步骤将正确重新创建默认命名空间。如果您对命名空间配置有信心,则可以跳过此步骤。

Second, if you still get Properties unrecognize as in my example where im trying to get data from sqlCon.

其次,如果您仍然无法识别属性,就像我在我尝试从 sqlCon 获取数据的示例中那样。

string connectionString = Properties.Settings.Default.sqlCon;

enter image description here

在此处输入图片说明

go to Project-> EventsandDelegates Propertiesas shown in the picture below. Make sure to create default settings file.

转到Project-> EventsandDelegates Properties,如下图所示。确保创建默认设置文件。

enter image description here

在此处输入图片说明

As soon as you create default settings under your Application settings, in my case it's SQL connection variable. Propertieserror will go away.

只要您在 Application settings 下创建默认设置,就我而言,它是 SQL 连接变量。Properties错误将消失。

enter image description here

在此处输入图片说明

回答by Arsen Khachaturyan

Funny, but in my case, I had defined the Propertiesin one projectin the solution and was trying to access from another.
Hope this might help someone.

有趣,但就我而言,我在解决方案Properties中的一个项目中定义了并试图从另一个.
希望这可以帮助某人。