具有GUI和控制台模式的WPF应用程序

时间:2022-04-28 18:09:43

I understand that there are problems regarding running a WPF application from a console window and outputting to the console rather than showing a gui (aka This Question).

我知道从控制台窗口运行WPF应用程序并输出到控制台而不是显示gui(又名本问题)存在问题。

The 'conclusion' of that question (which appears to be well researched and asked) is that it is possible to output from the application to the console, but it returns immediately - rather than blocking like a console application would.

该问题的“结论”(似乎已经得到很好的研究和提出)是可以从应用程序输出到控制台,但它会立即返回 - 而不是像控制台应用程序那样阻塞。

However, looking at Visual Studio particularly the 'devenv.exe' process, this is exactly what it does manage to do:

但是,查看Visual Studio特别是'devenv.exe'进程,这正是它设法要做的事情:

  • Double clicking on devenv.exe from within explorer runs the application gui only.
  • 从资源管理器中双击devenv.exe只运行应用程序gui。
  • From within a console (cmd.exe) window
    • devenv.exe<enter> runs the Gui returning to the console immediately
    • devenv.exe 运行Gui立即返回控制台
    • devenv.exe /?<enter> displays the help within the console, only returning after all output has been displayed.
    • devenv.exe /? 显示控制台中的帮助,仅在显示所有输出后返回。
    • devenv.exe test.txt<enter> runs the Gui (loading test.txt) returning to the console immediately
    • devenv.exe test.txt 运行Gui(加载test.txt)立即返回控制台
  • 在控制台(cmd.exe)窗口内,devenv.exe 运行Gui,立即返回控制台devenv.exe /? 显示控制台内的帮助,仅在显示所有输出后返回。 devenv.exe test.txt 运行Gui(加载test.txt)立即返回控制台

So, it must be possible to have this behaviour in our own applications somehow! Does anyone know how?

因此,必须有可能以某种方式在我们自己的应用程序中出现这种行为!有谁知道怎么样?

2 个解决方案

#1


4  

On an informational note, this article here explains how Visual Studio does it. I quote:

在信息性的说明中,这篇文章解释了Visual Studio如何做到这一点。我引用:

In VisualStudio case, there are actually two binaries: devenv.com and devenv.exe. Devenv.com is a Console app. Devenv.exe is a GUI app. When you type devenv, because of the Win32 probing rule, devenv.com is executed. If there is no input, devenv.com launches devenv.exe, and exits itself. If there are inputs, devenv.com handles them as normal Console app.

在VisualStudio案例中,实际上有两个二进制文件:devenv.com和devenv.exe。 Devenv.com是一款控制台应用。 Devenv.exe是一个GUI应用程序。当您键入devenv时,由于Win32探测规则,会执行devenv.com。如果没有输入,devenv.com将启动devenv.exe,并退出。如果有输入,devenv.com会将它们作为普通的控制台应用程序处理。

#2


1  

Go into your App.xaml and remove the Startup Uri and all references to your main Application Window which is started.

进入App.xaml并删除Startup Uri以及对启动的主Application Window的所有引用。

You can override the OnStartup Uri. See this Question: WPF Command Line

您可以覆盖OnStartup Uri。看到这个问题:WPF命令行

#1


4  

On an informational note, this article here explains how Visual Studio does it. I quote:

在信息性的说明中,这篇文章解释了Visual Studio如何做到这一点。我引用:

In VisualStudio case, there are actually two binaries: devenv.com and devenv.exe. Devenv.com is a Console app. Devenv.exe is a GUI app. When you type devenv, because of the Win32 probing rule, devenv.com is executed. If there is no input, devenv.com launches devenv.exe, and exits itself. If there are inputs, devenv.com handles them as normal Console app.

在VisualStudio案例中,实际上有两个二进制文件:devenv.com和devenv.exe。 Devenv.com是一款控制台应用。 Devenv.exe是一个GUI应用程序。当您键入devenv时,由于Win32探测规则,会执行devenv.com。如果没有输入,devenv.com将启动devenv.exe,并退出。如果有输入,devenv.com会将它们作为普通的控制台应用程序处理。

#2


1  

Go into your App.xaml and remove the Startup Uri and all references to your main Application Window which is started.

进入App.xaml并删除Startup Uri以及对启动的主Application Window的所有引用。

You can override the OnStartup Uri. See this Question: WPF Command Line

您可以覆盖OnStartup Uri。看到这个问题:WPF命令行