名称Application在当前上下文中不存在

use*_*291 5 c#

我有一个现有的winform应用程序,它没有使用Program.cs.我想用通常的代码添加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());
    }
}
Run Code Online (Sandbox Code Playgroud)

但我有错误信息

应用程序在当前上下文中不存在.

怎么解决这个?

Fre*_*örk 11

您需要在文件的开头有一个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());
    }
}
Run Code Online (Sandbox Code Playgroud)

要么是这样,要么修改Main方法以使用完整类型名称:

System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new Form1());
Run Code Online (Sandbox Code Playgroud)

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