wpf 将 C# 控制台应用程序与 GUI 集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38903150/
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
Integrate a C# console application with a GUI
提问by andres_v
I've been developing using C# from scratch for less than 3 months and what I got at present is a console application made with Visual Studio 2015. This application consumes a web service, the XML is deserialized, formatted and saved in an Access database, and lastly a success or error XML is sent back to the web server. 3 classes make up the application: The main stream class, a SOAP client class and a DB class.
我从头开始使用 C# 开发不到 3 个月,目前我得到的是一个使用 Visual Studio 2015 制作的控制台应用程序。这个应用程序使用一个 web 服务,XML 被反序列化、格式化并保存在 Access 数据库中,最后将成功或错误的 XML 发送回 Web 服务器。应用程序由 3 个类组成:主流类、SOAP 客户端类和 DB 类。
Now the reason why I'm posting this it's because I need to get that console app integrated with a GUI in which some data gotten from the deserialized XML should be shown (this is an extension of the project that the stakeholders didn't think about before stepping on the developing stage), but I have no idea how to do it.
现在我发布这个的原因是因为我需要将该控制台应用程序与 GUI 集成,其中应该显示一些从反序列化的 XML 中获取的数据(这是项目的扩展,利益相关者没有考虑在踏上开发阶段之前),但我不知道该怎么做。
So far I am reading about "Building WPF Applications"and "Windows Forms"but the documentation is overwhelming and I don't know if I'm on the right path, so can you guys give some guidelines before I start wasting time coding stuff that maybe are not the best option to integrate my console application with a GUI? Is there a way to make the console application become a GUI based application?
到目前为止,我正在阅读有关“构建 WPF 应用程序”和“Windows 窗体”的内容,但文档太多了,我不知道我是否走在正确的道路上,所以你们能在我开始浪费时间编码之前提供一些指导吗这可能不是将我的控制台应用程序与 GUI 集成的最佳选择?有没有办法让控制台应用程序成为基于 GUI 的应用程序?
I would appreciate if you provide me with practical links to tutorials in which they quickly implement GUIs, my time is running out and I need to start coding right now. Thank you very much for reading this and helping me.
如果您向我提供指向教程的实用链接,他们可以在其中快速实现 GUI,我将不胜感激,我的时间不多了,我需要立即开始编码。非常感谢您阅读本文并帮助我。
采纳答案by user2647513
If you're just looking for a barebones GUI and aren't too worried about it looking polished, I'd suggest you right click on your project -> add->Windows Form.
如果您只是在寻找准系统 GUI 并且不太担心它看起来很漂亮,我建议您右键单击您的项目-> 添加-> Windows 窗体。
Then, turning your your Console App into a GUI based application is as simple as instantiating your Form-derived class and calling .ShowDialog().
然后,将您的控制台应用程序转换为基于 GUI 的应用程序就像实例化您的表单派生类并调用 .ShowDialog() 一样简单。
Example:
例子:
using System.Windows.Forms;
//Note: if you create a new class by clicking PROJECT->Add Windows Form,
//then this class definition and the constructor are automatically created for you.
//Open MyFancyForm.cs, right click on the form, and click View Code to see it.
public partial class MyFancyForm : Form
{
public MyFancyForm()
{
InitializeComponent();
}
}
static void Main(string[] args)
{
var guiForm = new MyFancyForm();
guiForm.ShowDialog();//This "opens" the GUI on your screen
}
It's that simple. I suggest you add add a new class to your project as a WindowsForm instead of just a class so that you have access to the form designer. Of course, it's up to you to add fields to your form class and have it populate the GUI appropriately.
就这么简单。我建议您将一个新类添加到您的项目中作为 WindowsForm 而不是一个类,以便您可以访问表单设计器。当然,由您向表单类添加字段并让它适当地填充 GUI。
Edit: if you wish replace the Console entirely, right click on your project->properties and then change "output type" to Windows Application.
编辑:如果您希望完全替换控制台,请右键单击您的项目-> 属性,然后将“输出类型”更改为 Windows 应用程序。
回答by Timo Salom?ki
What you're trying to achieve is actually relatively simple and straight forward. You need to launch your existing console application process from a new Windows Formsor WPFapplication and subscribe to its input and output streams.
您要实现的目标实际上相对简单直接。您需要从新的Windows Forms或WPF应用程序启动现有的控制台应用程序进程并订阅其输入和输出流。
If you don't want to show your current console application as a visible window, remember to set CreateNoWindowto true.
如果您不想将当前控制台应用程序显示为可见窗口,请记住将CreateNoWindow设置为 true。
var processStartInfo = new ProcessStartInfo(fileName, arguments);
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.CreateNoWindow = true;
// etc.
Here's an article that goes through all the steps: Embedding a Console in a C# Application
这是一篇完成所有步骤的文章:在 C# 应用程序中嵌入控制台
The above article's code can be found on GitHubif you want to take a closer look. Here's an example of what sort of embedding can be done:
上面文章的代码可以在GitHub上找到,如果你想仔细看看。以下是可以完成哪种嵌入的示例:
With the project it's easy for you to either hide the whole console or have it side to side with some custom UI controls that do further processing with your data.
通过该项目,您可以轻松隐藏整个控制台,或者将其与一些自定义 UI 控件并排放置,以便对您的数据进行进一步处理。
回答by Vincent L.
You can put a TextBox inside your form and redirect anything that would go in the console to the TextBox.
您可以在表单中放置一个 TextBox,并将控制台中的任何内容重定向到 TextBox。
see this for more info Redirecting Console.WriteLine() to Textbox
请参阅此以获取更多信息 Redirecting Console.WriteLine() to Textbox
回答by qazcde
Without entering in "why" territories, if you have a console application project you can right click the Project -> Properties -> Application and change the Output type to Class Library (or to Widows Application).
无需输入“为什么”领域,如果您有控制台应用程序项目,您可以右键单击项目 -> 属性 -> 应用程序并将输出类型更改为类库(或 Widows 应用程序)。
If you choose class library then you can consume your library from wherever.
如果您选择类库,那么您可以从任何地方使用您的库。


