This question already has an answer here:
- WPF Command Line Arguments, a smart way? 2 answers
So from this post I got the following code for passing command line arguments into my WPF Application
public partial class App : Application
{
private string _customerCode;
public string CustomerCode
{
get { return _customerCode; }
set { _customerCode = value; }
}
protected override void OnStartup(StartupEventArgs e)
{
if (e.Args.Count() != 0)
{
CustomerCode = e.Args[0].Replace("/", string.Empty);
}
}
}
The application then starts my MainWindow.xaml and the application runs but in the viewModel for MainWindow.xaml (MainViewModel.cs) I would like to access the value of App.CustomerCode.
Is this the right way to deal with command line arguments and is it possible to access the value of CustomerCode?
One of the easy ways to access the customer code is to overwrite the
Application.Current
property with thenew
keyword (as more or less Davut has pointed out in his answer):In your view model you can then access the customer code by writing
App.Current.CustomerCode
.However, if you want a more object-oriented way regarding the SOLID principles, I would advise to do the following: in your
OnStartup
method, create the view model and the main window in code and show it. Using this approach, you can inject the customer code to the view model, e.g. via constructor injection. TheOnStartup
method would look like this:For this to work, you have to remove the
StartupUri
entry inApp.xaml
- otherwise your manually created main window will not show up.Your view model would look like this:
This approach is a more flexible than to access the static
App.Current
property as your view model is independent of theApp
class (i.e. it has no references to it).If you want to learn more about dependency injection, just google it, you'll find plenty of example. I can also recommend the excellent book Dependency Injection in .NET by Mark Seemann, if you want to dive in more.
I do not know your real intent, but the code below should help you.
You can try this by adding some arguments on project debug settings.
If your parameters contains blank spaces you should use "" signs to distinguish them. But instead of doing this you can use Application Config file for some cases. You can add some Settings from Project Settings or directly editing by settings.settings file.
If you need/fancy startup args, here is it.