[英]WPF Command Line and MvvmLight with designdata
I want to override the OnStartup like it is explained in this thread 我想重写OnStartup,如本线程中所述
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: 现在是我正在使用MVVM Light Toolkit的问题,该工具抛出XamlParseException,此时,“ Locator”是未知的:
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? 如何将命令行与MVVM Light Toolkit结合使用?
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? 但是,如果xaml中已经声明了ViewModelLocator,为什么还要将它添加到资源中呢?
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();
}
you have to check if the resource contains already the locator 您必须检查资源是否已经包含定位器
ViewModelLocator locator;
if (!Resources.Contains("Locator"))
{
locator = new ViewModelLocator();
Resources.Add("Locator", locator);
}
else
{
locator = (ViewModelLocator) Resources["Locator"];
}
WorkingWindow mainWindow = new WorkingWindow();
mainWindow.ShowDialog();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.