如何将 .NET 控制台应用程序转换为 Winforms 或 WPF 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/144701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:07:32  来源:igfitidea点击:

How do I convert a .NET console application to a Winforms or WPF application

.netwinformsconsole

提问by Jon Galloway

I frequently start with a simple console application to try out an idea, then create a new GUI based project and copy the code in. Is there a better way? Can I convert my existing console application easily?

我经常从一个简单的控制台应用程序开始尝试一个想法,然后创建一个新的基于 GUI 的项目并复制代码。有没有更好的方法?我可以轻松转换现有的控制台应用程序吗?

回答by albertein

Just add a new Winform, add the following code to your Main:

只需添加一个新的 Winform,将以下代码添加到您的Main

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());

Also, be sure the [STAThread]attribute is declared above your Mainfunction to indicate the COM threading model your Windows application will use (more about STAThread here).

此外,请确保在[STAThread]您的Main函数上方声明了该属性,以指示您的 Windows 应用程序将使用的 COM 线程模型(更多关于 STAThread在这里)。

Then right click your project and select properties and change the "Output type" to Windows application and you're done.

然后右键单击您的项目并选择属性并将“输出类型”更改为 Windows 应用程序,您就完成了。

EDIT :

编辑 :

In VS2008 the property to change is Application type

在 VS2008 中,要更改的属性是 Application type

enter image description here

在此处输入图片说明

回答by dbruning

For completeness - and for other newbs like me - you also need to add:

为了完整性 - 以及像我这样的其他新手 - 您还需要添加:

using System.Windows.Forms;

使用 System.Windows.Forms;

... to the top of Program.cs

... 到 Program.cs 的顶部