2010-10-31 72 views
4

我有一个现有的winform应用程序没有使用Program.cs。我想与常规码名称应用程序在当前上下文中不存在

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

添加Program.cs的,但我有错误讯息

应用程序不能在当前的背景下存在。

如何解决这个问题?

回答

10

你需要有一个using指令为System.Windows.Forms在文件的开头:

using System.Windows.Forms; 

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

要么,或修改Main方法使用完整的类型名称:

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

。虽然我会推荐第一个解决方案。

1

Application class住在System.Windows.Forms命名空间中。您需要在该文件的顶部添加引用,或明确指定路径的质量。

1

确保您添加了对System.Windows.Forms程序集的引用并添加了对System.Windows.Forms名称空间的引用。

1

Program.cs文件是否包含using System.Windows.Forms;Application类在该名称空间中。

2

如果你使用VS,使用Alt + Shift + F10它会帮你,或者安装resharper

相关问题