I want to override the OnStartup like it is explained in this thread
Now is the problem that I'm using the MVVM Light Toolkit which throws a XamlParseException ,which says that the "Locator" isn't known, on this point:
DataContext="{Binding Main, Source={StaticResource Locator}}
I have no problem to design time
App.xaml
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
</Application.Resources>
My override
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
if (e.Args.Length > 0 && e.Args[0] == "\\start")
{
/* do stuff without a GUI */
MessageBox.Show("Start");
}
else
{
MainWindow mainWindow = new MainWindow(); // <-- Exception
ViewModelLocator locator = new ViewModelLocator();
mainWindow.DataContext = locator.Main;
mainWindow.ShowDialog();
}
this.Shutdown();
}
How can I use command line in combination with the MVVM Light Toolkit?
Update 13.02.2013 10:10
With this Override there is no longer an exception. But why I have to add the ViewModelLocator to the resources if it is already declared in the xaml?
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
if (e.Args.Length > 0 && e.Args[0] == "\\start")
{
/* do stuff without a GUI */
MessageBox.Show("Start");
}
else
{
ViewModelLocator locator = new ViewModelLocator();
Resources.Add("Locator", locator);
MainWindow mainWindow = new MainWindow();
//DataContext="{Binding Main, Source={StaticResource Locator}}"
//mainWindow.DataContext = locator.Main;
mainWindow.ShowDialog();
}
this.Shutdown();
}