【问题标题】:WPF Command Line and MvvmLight with designdataWPF 命令行和 MvvmLight 与 designdata
【发布时间】:2013-02-13 06:33:46
【问题描述】:

我想重写 OnStartup,就像在这个线程中解释的那样

WPF Command Line

现在的问题是我正在使用 MVVM Light Toolkit 引发 XamlParseException ,它表示“定位器”未知,在这一点上:

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

设计时间没问题

App.xaml

<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

有了这个覆盖,不再有例外。但是,如果 ViewModelLocator 已经在 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();
}

【问题讨论】:

    标签: c# .net mvvm command-line mvvm-light


    【解决方案1】:

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

    ViewModelLocator locator;
    if (!Resources.Contains("Locator"))
    {
        locator = new ViewModelLocator();
        Resources.Add("Locator", locator);
    }
    else
    {
        locator = (ViewModelLocator) Resources["Locator"];
    }
    
    WorkingWindow mainWindow = new WorkingWindow();
    mainWindow.ShowDialog();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-30
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      相关资源
      最近更新 更多