windows .net 中的控制台用户界面是否可行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5079393/
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
Is a console user interface in .net possible?
提问by mark
I am wondering whether there are libraries in .NET that make it easy to write console user interfaces. For instance, imagine a WinForms application, where the user can:
我想知道 .NET 中是否有可以轻松编写控制台用户界面的库。例如,想象一个 WinForms 应用程序,用户可以在其中:
- Navigate the menus
- Respond to dialogs
- 导航菜单
- 响应对话框
And the application in return displays several lines of text.
应用程序反过来显示多行文本。
It can be shown, that the GUI in such an application can be mapped to the respective CUI without any problem.
可以证明,此类应用程序中的 GUI 可以毫无问题地映射到相应的 CUI。
So, if one has to stick to console, then are there any .NET tools to let write such CUI easily?
那么,如果必须坚持使用控制台,那么是否有任何 .NET 工具可以轻松编写这样的 CUI?
EDIT1
编辑1
Let me define a constraint, which should help folks to grasp the idea of CUI. Imagine a machine, to which you can open a remote console, but not RDS. So, running a GUI application remotely on that machine is out of the question, because it will be unable to open any window. However, it is possible to have a remote console, leaving us with two possible flavors of CUI:
让我定义一个约束,它应该可以帮助人们理解 CUI 的概念。想象一台机器,您可以向其打开远程控制台,但不能打开 RDS。因此,在该机器上远程运行 GUI 应用程序是不可能的,因为它将无法打开任何窗口。但是,可以有一个远程控制台,给我们留下两种可能的 CUI 风格:
- Character based graphics, like old supermarket terminals
- Completely text based.
- 基于字符的图形,如旧的超市终端
- 完全基于文本。
The first options allows to place characters at arbitrary positions on the console window. ncurses is the low level library for *nix systems that allows to do such things. A CUI created in such a way can be pretty expressive and convenient, but for the sake of our discussion let me rule this option out as well, because a remote console is unlikely to support the ability to move around the console window.
第一个选项允许在控制台窗口的任意位置放置字符。ncurses 是 *nix 系统的低级库,它允许做这样的事情。以这种方式创建的 CUI 可能非常具有表现力和方便,但为了我们的讨论,让我也排除此选项,因为远程控制台不太可能支持在控制台窗口中移动的能力。
So, this leaves us with the text based CUI, the one created by means of printf
and scanf only (and the likes).
因此,这给我们留下了基于文本的 CUI,printf
仅通过和 scanf(以及类似方法)创建的 CUI 。
EDIT2
编辑2
Another clarification: I mention Windows Forms in the question as an example of simple User Interface, which has nothing special that could not be translated to text based console UI. This is only to illustrate that simple GUI elements like menus and modal dialogues can be modeled in console without resorting to windows.
另一个澄清:我在问题中提到了 Windows 窗体作为简单用户界面的示例,它没有任何特殊之处,无法转换为基于文本的控制台 UI。这只是为了说明简单的 GUI 元素(如菜单和模态对话框)可以在控制台中建模,而无需求助于窗口。
采纳答案by Maxim Gueivandov
Have a look at consolemenuui.codeplex.com. Other than menus (plus Yes-No dialogs and value prompts which should be easy to implement), I could hardly imagine other possible prompt-based controls.
看看consolemenuui.codeplex.com。除了菜单(加上 Yes-No 对话框和应该很容易实现的值提示),我几乎无法想象其他可能的基于提示的控件。
回答by kenny
How about Mono Curses. http://www.mono-project.com/MonoCurses
回答by Maxim Gueivandov
What about Forms for Console Apps?
控制台应用程序的表单怎么样?
Alternative (aka ugly workaround) solution could be to create a web application (which will give you more flexibility over your UI) and access it with a browser which runs under DOS. That web application could be hosted on an external web server (IIS), or hosted in your console applicationwhich could also launch a DOS-based browser on start-up for user convenience.
替代(又名丑陋的解决方法)解决方案可能是创建一个 Web 应用程序(这将为您的 UI 提供更大的灵活性)并使用在 DOS 下运行的浏览器访问它。该 Web 应用程序可以托管在外部 Web 服务器 (IIS) 上,也可以托管在您的控制台应用程序中,该应用程序还可以在启动时启动基于 DOS 的浏览器,以方便用户使用。
回答by SK-logic
A typical approach is to split your application into two parts: a backend, which can be operated via CLI, and a GUI frontend (may be more than one, e.g., web, winforms, a pure text CLI, whatever). They can communicate using a simple protocol, or a DSL - frontend asks the backend to execute commands, and the backend forces the frontend to execute commands in turn (so, the protocol must be asyncronous). If some complex visualisation is required, it is still possible to prepare it in a backend and execute in a frontent. An obvious thing to avoid doing this way is streaming video, of course.
一种典型的方法是将您的应用程序分成两部分:可以通过 CLI 操作的后端和 GUI 前端(可能不止一个,例如 web、winforms、纯文本 CLI,等等)。它们可以使用简单的协议或 DSL 进行通信——前端要求后端执行命令,后端强制前端依次执行命令(因此,协议必须是异步的)。如果需要一些复杂的可视化,仍然可以在后端准备它并在前端执行。当然,避免这样做的一个显而易见的事情是流式传输视频。
Probably the best example of such a design is Wolfram Mathematica.
这种设计的最好例子可能是 Wolfram Mathematica。
So, answering your question, you won't need any specific libraries, it is a matter of design, coding itself is trivial. The only thing you may need is a tool that will simplify your DSLs implementation. Antlr is just fine, but with C# it is easy to parse simple languages without any external parser generators.
所以,回答你的问题,你不需要任何特定的库,这是一个设计问题,编码本身是微不足道的。您可能唯一需要的是一个可以简化 DSL 实现的工具。Antlr 很好,但是使用 C# 很容易解析简单的语言,而无需任何外部解析器生成器。
回答by user6179188
Something like this maybe?
也许像这样的东西?
I am new to C# and coding in general but grew up with a c64 and love the retro style, so I'm writing a overkill memopad/consoleprogram. It draws a menu using an array and a box around it, same goes for text files.
我是 C# 和一般编码的新手,但在 c64 中长大并且喜欢复古风格,所以我正在编写一个矫枉过正的备忘录/控制台程序。它使用一个数组和一个围绕它的框绘制一个菜单,文本文件也是如此。
回答by Please treat your mods well.
You can try to capture/redirect the output of your own hidden console window to your applications's Windows Forms. Once you have the text you need, you can print it with PictureBox.DrawString
or a similar method.
您可以尝试将自己隐藏的控制台窗口的输出捕获/重定向到应用程序的 Windows 窗体。获得所需的文本后,您可以使用PictureBox.DrawString
或类似的方法打印它。
Here's more infoabout how to capture/redirect console output.
回答by Dan Abramov
There's a wonderful library for handling command line options, called NDesk.Options
. Being included in Mono, it is also known as Mono.Options
.
It doesn't provide any additional interaction capabilities, however this is the closest I found to elegantly dealing with console from .NET app.
有一个很棒的用于处理命令行选项的库,称为NDesk.Options
. 包含在 Mono 中,它也被称为Mono.Options
.
它不提供任何额外的交互功能,但是这是我发现的最接近从 .NET 应用程序优雅地处理控制台的方法。
string data = null;
bool help = false;
int verbose = 0;
var p = new OptionSet {
{ "file=", v => data = v },
{ "v|verbose", v => { ++verbose } },
{ "h|?|help", v => help = v != null },
};
List<string> extra = p.Parse (args);
As for libraries that encapsulate console UI programming, I didn't find any, although I'd like to hear about one.
至于封装控制台 UI 编程的库,我没有找到任何库,但我想听听一个。
回答by user1941371
This is a link to a library (Console.Forms) that attempts to create a Console equivalent to System.Windows.Forms:
这是一个指向库 (Console.Forms) 的链接,该库尝试创建与 System.Windows.Forms 等效的控制台:
https://code.google.com/p/console-forms/
https://code.google.com/p/console-forms/
It is far from complete, but I think it is going in the direction you're talking about. While there are labels and a half-finished text-box, there are no menus yet. It does demonstrate opening a dialog in front of a main form.
它远未完成,但我认为它正在朝着你所说的方向发展。虽然有标签和半成品的文本框,但还没有菜单。它确实演示了在主窗体前打开一个对话框。