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.
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.
WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).
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.
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.
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(); }
To check for the existence of your argument - in Matt's solution use this for your test:
e.Args.Contains("MyTriggerArg")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With