【问题标题】:.NET C# main method and windows forms.NET C# main 方法和 windows 窗体
【发布时间】:2013-08-09 23:35:30
【问题描述】:

我有一个使用 C# 的 Visual Studio 项目,它可能是 Console Application。当我尝试运行/构建/调试项目时,它会查找现有类中的 Main 方法。我在该项目上添加了一个 Windows 窗体,我希望它在 Windows 窗体版本中运行,而不是在命令行中(期望参数)。您能告诉我如何编辑项目的运行时间以查找 Windows 窗体而不是 static void main()

【问题讨论】:

  • 也许这是一个愚蠢的问题,但您是否将 Windows 窗体项目声明为“启动项目”?
  • 可以更改启动对象吗? (项目属性)
  • 您的 windowsForms App Main 方法应该在那里,并将您的 windowsForms App 设置为 StartUpProject

标签: c# .net main


【解决方案1】:

方法一

在主函数中使用以下内容:

Application.Run(new Form1());

您还需要在文件顶部添加以下行:

using System.Windows.Forms;


方法二

在主函数中,你可以添加:

Form1 c = new Form1();
c.ShowDialog();


这两种方法都会将您的表单显示为对话框。但是,控制台仍将在后台可见。

如果你想隐藏控制台窗口,下面的链接给出了这样做的说明(但它有点复杂):

http://social.msdn.microsoft.com/Forums/vstudio/en-US/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd/hide-console-window-in-c-console-application

【讨论】:

  • 我会试一试并告诉你。
  • 我无法获取应用程序。你知道为什么吗?
  • 虽然它有效(感谢您的一百万的帮助),但表单至少需要 5 秒才能显示出来,您知道为什么吗?
  • 这取决于您在调用表单加载之前在表单加载和主函数中所做的事情。如果里面什么都没有,那么它应该很快。
【解决方案2】:

将您的 Programe.cs 文件更改为

  class Program
  {
    [STAThread]
    static void Main()
    {
        //
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //
        Application.Run(new Form1());

    }
  }

然后右键单击Console project 并转到properties 并将Output Type 设置为Windows 应用程序

【讨论】:

  • 当我尝试你的建议时,我得到了:The name Application does not exist in the current context
  • 我想我明白了:using System.Windows.Forms;
  • 当你在你的项目中添加新的 Windows 窗体然后自动需要的 DLL 被添加到项目引用中,使用那些。
  • 当我为一个项目单击运行并且它构建成功但没有显示任何内容时是什么意思。
  • @Doug Hauf 你错过了 Application.Run(new Form1());在你的 program.cs 文件中.. 在你的项目中添加一个 windows 窗体并从你的 program.cs 文件中初始化该窗体的新构造函数
【解决方案3】:

您可以更改类型project

小修正:Visual Studio 不跟踪项目 用于创建项目的模板。项目系统主要是 不知道用于项目的初始模板。有几种 项目系统中的项目(例如项目类型)具有 与特定模板同名,但这是巧合,两者 没有得到明确的纠正。

The only thing that can really be changed in terms of the project type is essentially the output type. This can have value Class

库、控制台应用程序和 Windows 应用程序。你可以改变 通过转到项目属性页面(右键单击属性) 并更改输出类型组合框。

It is possible to have other project types supported by the project system but they are fairly few and are not definitively associated with a project template.

【讨论】:

  • 我已经这样做了,但是当我按 F5 时,它仍然在寻找 Main Method。我需要它以我的形式而不是 MAIN() 开始。
  • 在main方法中可以从表单中获取实例
  • 所以你是说让应用程序从我添加的 Windows 窗体运行的唯一方法是通过 MAIN 方法调用它们?
【解决方案4】:

在创建新的控制台应用程序时,默认行为是添加一个名为 Program 的类和一个名为 Main 的方法

类似的东西。

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

而 Windows 应用程序的默认设置是

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

【讨论】:

  • 这对我的问题有什么帮助。我知道它的样子。
  • 通过这样做,是的,我们可以运行Form1,但是如何隐藏在后台运行的Console window
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
相关资源
最近更新 更多