WPF/控制台混合应用 [英] WPF / Console Hybrid Application
问题描述
我编写了一个应用程序,它可以在命令行上运行,也可以使用 WPF UI.
I writing an application what can either be run on the command line, or with a WPF UI.
[STAThread]
static void Main(string[] args)
{
// Does magic parse args and sets IsCommandLine to true if flag is present
ParseArgs(args);
if(IsCommandLine)
{
// Write a bunch of things to the console
}
else
{
var app = new App();
app.Run(new Window());
}
}
我将项目的输出类型设置为控制台应用程序,如果我尝试通过双击 exe 来执行它,我会得到一个弹出的控制台窗口.如果未设置标志(通过命令 args 传入),我不想向用户显示控制台窗口.
I set the project's Output type to Console Application, I get a console window that popups if I try to execute it by double-clicking the exe. I don't want to show the console window to the user if the flag is not set (passed in via command args).
但是,如果我将项目的输出类型设置为 Windows 应用程序,双击行为是好的,但是当我在控制台中运行它时,却没有控制台输出 (Console.Writeline)
However, if I set the project's Output type to Windows Application, the double-click behaviour is fine, but when I run it in the console, I get no console output (Console.Writeline)
推荐答案
最好的办法是将实际执行工作的代码抽象到一个没有 UI 的单独类库中,然后在一个控制台中创建两个应用程序,调用它的其他 WPF.
Your best bet would be to abstract out the code that actually does the work to a separate class library that has no UI and then create two applications one Console, the other WPF that call this.
控制台应用程序和 WPF 应用程序具有完全不同的应用程序模型,因此您不能在两个应用程序中重用相同的代码.
A console application and an WPF application have entirely different application models so you can't reuse the same code in both applications.
拥有一个单独的类库可以让您做其他事情,例如在其他应用程序中使用它,例如网站或客户端/服务器架构.
Having a separate class library allows you do other things like use it in other applications such as a web site or client/server architecture.
这篇关于WPF/控制台混合应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!