繁体   English   中英

WPF命令行和带有designdata的MvvmLight

[英]WPF Command Line and MvvmLight with designdata

我想重写OnStartup,如本线程中所述

WPF命令行

现在是我正在使用MVVM Light Toolkit的问题,该工具抛出XamlParseException,此时,“ Locator”是未知的

DataContext="{Binding Main, Source={StaticResource Locator}}

我没有时间设计时间

应用程式

<Application.Resources>
    <!--Global View Model Locator-->
    <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />
</Application.Resources>

我的优先

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();
}

如何将命令行与MVVM Light Toolkit结合使用?

更新13.02.2013 10:10

有了此覆盖,就不再有例外。 但是,如果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();
}

您必须检查资源是否已经包含定位器

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM