One of the easy ways to access the customer code is to overwrite the Application.Current
property with the new
keyword (as more or less Davut has pointed out in his answer):
public class App : Application
{
public static new App Current
{
get { return (App) Application.Current; }
}
public string CustomerCode { get; set; }
protected override void OnStartup(StartupEventArgs e)
{
this.CustomerCode = e.Args[0].Replace("/", string.Empty);
base.OnStartup(e);
}
}
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. The OnStartup
method would look like this:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Composition root
var customerCode = e.Args[0].Replace("/", string.Empty);
// Here I inject the customer code to the view model so that
// it can save it in a field and use it later.
// This is called dependency injection (here it's constructor injection)
var mainWindowViewModel = new MainWindowViewModel(customerCode);
MainWindow = new MainWindow { DataContext = mainWindowViewModel };
MainWindow.Show();
}
For this to work, you have to remove the StartupUri
entry in App.xaml
- otherwise your manually created main window will not show up.
Your view model would look like this:
public class MainWindowViewModel
{
private readonly string _customerCode;
public MainWindowViewModel(string customerCode)
{
if (customerCode == null)
throw new ArgumentNullException("customerCode");
_customerCode = customerCode;
}
// Other code in this class can access the _customerCode field
// to retrieve the value from the command line
}
This approach is a more flexible than to access the static App.Current
property as your view model is independent of the App
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.