Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start WPF based on Arguments

Tags:

c#

wpf

I'm currently developing an application that does some file manipulation and I want to be able to do the manipulation through the console or via an UI (I chose WPF).

I pretty much want to say: (psuedo)

if ( Environment.GetCommandLineArgs().Length > 0 ) {     //Do not Open WPF UI, Instead do manipulate based     //on the arguments passed in } else {     //Open the WPF UI } 

I've read about a few different ways of starting the WPF Window/application programmatically like:

Application app = new Application (); app.Run(new Window1()); 

But I'm not entirely sure I want to just plug this into a Console Application.

Does anyone have best practices or recommendations on how I can achieve this? The main processing functionality is in a Helper class I created. So basically I either want a static start method (like standard Console Application creates) or the UI to access the Helper class depending on the arguments passed in.

like image 942
Tada Avatar asked Aug 01 '12 23:08

Tada


People also ask

How do I get command line arguments in WPF?

Right click on your WPF project in Solution Explorer and select properties. It will display the Window given below. Select Debug option and write the file path in the command line argument. Create a TXT file with Test.

Why is WPF so difficult?

The difficulty with learning WPF is not so much the API as the model. It's a very different mental model than you'd use with something like Windows Forms. Rather than writing methods that imperatively populate a UI element, you generally data bind to properties on an object.

Is WPF still relevant 2021?

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


2 Answers

In Application class there is an event "StartUp" you can use it . It provide you the args you provide through command prompt. Here is an example from MSDN:

App.xaml

<Application x:Class="WpfApplication99.App"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          Startup="App_Startup"> </Application> 

App.xaml.cs

public partial class App : Application {     void App_Startup(object sender, StartupEventArgs e)     {         // Application is running         // Process command line args         bool startMinimized = false;         for (int i = 0; i != e.Args.Length; ++i)         {             if (e.Args[i] == "/StartMinimized")             {                 startMinimized = true;             }         }          // Create main application window, starting minimized if specified         MainWindow mainWindow = new MainWindow();         if (startMinimized)         {             mainWindow.WindowState = WindowState.Minimized;         }         mainWindow.Show();     } } 

I hope this will help.

like image 178
yo chauhan Avatar answered Oct 14 '22 06:10

yo chauhan


There are 2 options to get the command line arguments
1) If you want to read the arguments OnStartup. This is good for global access of the args.

Override OnStartup in App.xaml.cs and look at the Args property of the StartupEventArgs class.

public partial class App : Application {     protected override void OnStartup(StartupEventArgs e)     {         foreach (string arg in e.Args)         {             // TODO: whatever         }         base.OnStartup(e);     } } 

2) Another easy way is to read the arguments from the Environment Object.

Environment.GetCommandLineArgs();

This can be used from anywhere in the application like from the Form / Page also.

like image 40
Habeeb Avatar answered Oct 14 '22 07:10

Habeeb