代码之家  ›  专栏  ›  技术社区  ›  user310291

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

c#
  •  5
  • user310291  · 技术社区  · 14 年前

    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());
        }
    }
    

    但我有个错误信息

    如何解决这个问题?

    5 回复  |  直到 14 年前
        1
  •  11
  •   Fredrik Mörk    14 年前

    你需要有一个 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());
    

    ……尽管我会推荐第一种解决方案。

        2
  •  2
  •   Andrew Bezzub    14 年前

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

        3
  •  1
  •   Ed Swangren    14 年前

    Application class 位于System.Windows.Forms命名空间中。您需要在该文件的顶部添加引用或显式地限定路径。

        4
  •  1
  •   pinkfloydx33    14 年前

    using System.Windows.Forms; ? 这个 Application 类位于该命名空间中。

        5
  •  1
  •   Saeed Amiri    14 年前

    如果您使用VS,使用Alt+Shift+F10它将帮助您,或者安装 resharper