Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Command Line

I am trying to create a WPF application that takes command line arguments. If no arguments are given, the main window should pop up. In cases of some specific command line arguments, code should be run with no GUI and exit when finished. Any suggestions on how this should properly be done would be appreciated.

like image 943
bingles Avatar asked Jan 08 '09 23:01

bingles


People also ask

What are WPF commands?

Commanding is an input mechanism in Windows Presentation Foundation (WPF) which provides input handling at a more semantic level than device input. Examples of commands are the Copy, Cut, and Paste operations found on many applications.

Is WPF still relevant 2021?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).

Is WPF phased out?

It may indeed get phased out eventually “The WPF's goal in user interface and graphics rendering in the . NET Framework 3.0 release is to provide a windowing system for the desktop environment on Microsoft Windows.

Is WPF still viable?

It was in 2006 that Windows Presentation Foundation (WPF) was released with . NET framework 3.0. Over the years it got improved and it is still now in the market in 2021.


2 Answers

First, find this attribute at the top of your App.xaml file and remove it:

StartupUri="Window1.xaml" 

That means that the application won't automatically instantiate your main window and show it.

Next, override the OnStartup method in your App class to perform the logic:

protected override void OnStartup(StartupEventArgs e) {     base.OnStartup(e);      if ( /* test command-line params */ )     {         /* do stuff without a GUI */     }     else     {         new Window1().ShowDialog();     }     this.Shutdown(); } 
like image 171
Matt Hamilton Avatar answered Nov 16 '22 02:11

Matt Hamilton


To check for the existence of your argument - in Matt's solution use this for your test:

e.Args.Contains("MyTriggerArg")

like image 23
GeekyMonkey Avatar answered Nov 16 '22 02:11

GeekyMonkey