简体   繁体   English

应用程序不包含定义

[英]Application does not contain a definition

I have a C# forms application.我有一个 C# 表单应用程序。 I import an additional CS file into the program and now it doesn't compile any more.我在程序中导入了一个额外的 CS 文件,现在它不再编译了。 Every time I try compile i get the following messages:每次我尝试编译时,我都会收到以下消息:

Application does not contain a definition for 'EnableVisualStyles'
Application does not contain a definition for 'SetCompatibleTextrenderingDefault'
Application does not contain a definition for 'Run'

When I click on the errors it bring me to Programs.cs.当我单击错误时,它会将我带到 Programs.cs。 It just contains the following information:它只包含以下信息:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

Any help would be appreciated.任何帮助,将不胜感激。

One more point, a test console app works fine, however I want this app to be forms application.还有一点,测试控制台应用程序工作正常,但是我希望这个应用程序成为表单应用程序。

You added a new using statement for a namespace that has another definition of Application , (it might be your own) and that is taking precedence.您为命名空间添加了一个新的using语句,该命名空间具有Application另一个定义(它可能是您自己的)并且具有优先级。 You can use the fully qualified name to be sure that you're targeting the right Application class:您可以使用完全限定名称来确保您的目标是正确的Application类:

global::System.Windows.Forms.Application.EnableVisualStyles();
global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
global::System.Windows.Forms.Application.Run(new Form1());

(You can omit the global:: as long as you don't also have a class named System but the above is just a tad more robust in the fact of ambiguous namespace/class names. (你可以省略global::只要你没有一个名为System的类,但上面的只是在不明确的命名空间/类名称的事实中更健壮一点。

Other alternatives include renaming the custom Application class, if it is indeed your own, not adding a using for whatever namespace it's in for this file (or any file that uses the System.Windows.Forms Application class), adding an alias for System.Windows.Forms.Application (this is done by something like using FormApplication = System.Windows.Forms.Application; ), etc.其他替代方法包括重命名自定义Application类(如果它确实是您自己的),而不是为该文件(或任何使用System.Windows.Forms Application类的文件)所在的任何命名空间添加usingSystem.Windows.Forms添加别名System.Windows.Forms.Application (这是通过using FormApplication = System.Windows.Forms.Application;类的东西using FormApplication = System.Windows.Forms.Application;完成的)等。

I ran into this problem when using the Entity Model Framework to connect to a database that happened to have a table named "Application".我在使用实体模型框架连接到恰好有一个名为“Application”的表的数据库时遇到了这个问题。 Once the classes were generated for the database, I immediately had two "Application" classes which caused the error in question.一旦为数据库生成了类,我立即有两个“应用程序”类导致了有问题的错误。 In my Program.cs file's Main() method, the following code change fixed the issue:在我的 Program.cs 文件的 Main() 方法中,以下代码更改修复了该问题:

System.Windows.Forms.Application.EnableVisualStyles();         
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new MyForm());

I know this is a necro-bump but it applies to my situation today.我知道这是一个死循环,但它适用于我今天的情况。 @James G's answer above made me realize that I had imported a web service that contained a definition for "Application" which also caused this error. @James G 上面的回答让我意识到我导入了一个包含“应用程序”定义的 Web 服务,这也导致了这个错误。 As part of your checking things, make sure that any imported services don't incidentally contain "Application."作为检查事项的一部分,请确保任何导入的服务不会附带包含“应用程序”。

If anyone is importing it, DynamicsSecurityAdminService is the one that did it to me.如果有人在导入它,DynamicsSecurityAdminService 就是对我做的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM