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

北城以北 提交于 2020-01-09 02:13:18

问题


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?


回答1:


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

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

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

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

EDIT :

In VS2008 the property to change is Application type




回答2:


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

using System.Windows.Forms;

... to the top of Program.cs



来源:https://stackoverflow.com/questions/144701/how-do-i-convert-a-net-console-application-to-a-winforms-or-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!