@BrunoKlein's answer will work, and I based my answer on his solution. Quoting @BrunoKlein,
First you have to use a WPF Application project and change the app.xaml so that you can override the window creation.
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Application.Resources>
</Application.Resources>
</Application>
Note this is missing the StartupUri property.
Now, even simpler (this works in Visual Studio 2015 at least), go to the project properties, and change the output type from Windows Application to Console Application. This makes the project build as a console app, but still has the capabilities of a Windows Application.
(Class Library is highlighted in this photo, select Console Application instead)
You did it! Done.
Now, instead of having a void Main(string[] args)
, your "main" method is the OnStautup
method of your autogenerated App
class:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
string[] args = e.Args;
if (SomeConditionBasedOnTheArgs(args))
{
// Instantiate view, call View.Show()
}
else
{
// Process the args
}
}
}
Note the one difference between this answer and @BrunoKlein's answer is that this one will always "show" a console if it is run from explorer/start menu/desktop, but if run from the command line, it will run and direct all of its standard output to that console, just like any normal console application.