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

How do I convert a .NET console application to a Winforms or WPF application(如何将 .NET 控制台应用程序转换为 Winforms 或 WPF 应用程序)
本文介绍了如何将 .NET 控制台应用程序转换为 Winforms 或 WPF 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

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

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

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).

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

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

在 VS2008 中要更改的属性是应用程序类型

In VS2008 the property to change is Application type

这篇关于如何将 .NET 控制台应用程序转换为 Winforms 或 WPF 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
How to store delegates in a List(如何将代表存储在列表中)
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
CreateDelegate with unknown types(具有未知类型的 CreateDelegate)
Does Funclt;Tgt;.BeginInvoke use the ThreadPool?(Funclt;Tgt;.BeginInvoke 使用线程池吗?)