2010-10-31 71 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

相關問題