简体   繁体   English

C#控制台和GUI应用程序合而为一

[英]C# Console and GUI Application in one

This is in continuation to my earlier question here myPost . 这是我之前的问题myPost的延续 That was a kind of a flop question. 这是一个失败的问题。 Somehow we will modify that in Java and use it. 我们将以某种方式在Java中对其进行修改并使用它。

To move further, I have decided to re-do that project in C# which I am more comfortable with. 为了更进一步,我决定使用我更喜欢的C#重新执行该项目。 To begin with I hit a roadblock. 首先,我遇到了障碍。 In that java application, the actual coding is done in Eclipse and the .jar can be executed through command line. 在该Java应用程序中,实际的编码在Eclipse中完成,.jar可以通过命令行执行。 Also a GUI is done using Netbeans which refers to the actual program in Eclipse. 另外,使用Netbeans来完成GUI,它引用了Eclipse中的实际程序。 So the same appl is used as a command line as well as GUI. 因此,同一应用程序既用作命令行又用作GUI。

How can I do this in C# ?? 如何在C#中执行此操作? Just write the code once and then execute .exe or through GUI, but both will work independently. 只需编写一次代码,然后执行.exe或通过GUI执行,但两者均可独立工作。

In the .NET world, this is often done by putting the "guts" of the application in a library assembly which is then used by both the GUI and console application projects. 在.NET世界中,通常是通过将应用程序的“内容”放入库程序集中来完成的,然后由GUI和控制台应用程序项目使用。 So for example, you might set up a solution with three projects: 因此,例如,您可以使用三个项目设置一个解决方案:

  1. A Windows Form application Windows窗体应用程序
  2. A console application 控制台应用程序
  3. An assembly library with that does the actual work 具有实际功能的程序集库

The best way to create an "hybrid" application would be to decouple your logic from your UI representation (MVC, MVP or MVVM). 创建“混合”应用程序的最佳方法是将逻辑与UI表示(MVC,MVP或MVVM)分离。 This will create two .exe (one in GUI, one in command line) and a dll which will include the Business Logic. 这将创建两个.exe(一个位于GUI中,一个位于命令行中)和一个dll,其中将包含业务逻辑。

This way, you're also making sure that your application can be more easily modified and maintained in the future. 这样,您还可以确保将来可以更轻松地修改和维护您的应用程序。

Create a C# class library that contains public methods that do the work. 创建一个C#类库,其中包含完成工作的公共方法。 You can then reference the C# class library from any interface you'd like to offer the user. 然后,您可以从任何想要提供给用户的接口中引用C#类库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM