不包含适用于入口点的静态“main”方法。

127

今天我开始将我的代码组织成单独的.cs文件,为了让与UI相关的方法能够继续工作,我会在相同的命名空间和公共分部类名称下创建.cs代码,以便这些方法可以互操作。

我的头文件长这样,在包括调用的主要核心文件在内的四个文件中:

public shell()
{
InitializeComponent(); 
}

与UI配合工作的.cs文件的头部区域(似乎是导致这种新冲突的原因):

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Threading;
using System.Collections.Specialized;
using System.Net;
using System.Runtime.InteropServices;
using watin = WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
using System.Web; 


namespace WindowsFormsApplication1
{

    public partial class shell : Form
    {

现在当我试图调试/预览我的应用程序(顺便说一下,这是Visual Studio 2010 Express中的Windows应用程序)时,我收到了以下错误消息:

不包含适合入口点的静态“main”方法

我查看了应用程序属性中的Application->Startup object,但它没有提供任何选项。如何告知应用程序从具有我的InitializeComponent();命令的.cs文件开始?

  • 我已经搜索了很久,但还没有找到解决方案。
  • 每个.cs文件上的属性都设置为“编译”。
  • 我在我的解决方案资源管理器中没有看到App.xaml文件,但我确实看到一个app.config文件。

我还是很新手,这是我尝试使用C#代码进行组织的第一次尝试。


13
你有一个主方法吗? - bobek
1
你需要一个名为main的静态方法,并且签名正确。这样编译器就知道如何启动你的程序。 - David Heffernan
有没有办法我可以手动编程解决方案从哪里开始?我很困惑为什么它找不到静态的“Main”方法,因为我似乎找不到任何指示代码告诉它查找“Main”方法。我对c#非常陌生。 --- [编辑] - @David Hefferman,您能详细说明“正确签名”的含义吗? - atwellpub
6
尝试将类似以下代码段添加到您的项目中:[STAThread] static void Main(string[] args) { Application.Run(new shell()); } 这样做可以确保在单线程单元 (STA) 中运行应用程序,并启动名为shell的新窗体。 - L.B
1
@L.B:将其创建为答案,这样他就可以接受它。 - Joshua
显示剩余3条评论
33个回答

0

添加

static async Task Main(string[] args)
{
}

替代

static async void Main(string[] args)
{
}

这对我有效。


0

如果你和我一样,可能最开始是用类库(Class Library)编写程序,后来转而使用控制台应用程序(Console Application)。如果是这样,请进行以下更改...

namespace ClassLibrary1
{
    public class Class1
    {
    }
}

变成这样...

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

-1

也许是无意中的,但将我的Docker文件移动到解决方案文件夹而不是项目文件夹中消除了错误。当我仍然想独立于Docker运行解决方案时,这非常有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接