【问题标题】:MVVM Light ViewModelLocator in separate assembly ? Design time mode issueMVVM Light ViewModelLocator 在单独的程序集中?设计时模式问题
【发布时间】:2012-02-09 17:46:43
【问题描述】:

我正在使用带有 Ninject 的 MVVM Light V4。我的 ViewModel 文件在单独的程序集中。当 ViewModelLocator 处于启动程序集(查看文件)时效果很好。可混合性有效。

我想将 ViewModelLocator 放在 ViewModel 程序集中,但是当我这样做时,我失去了可混合性。之后它只能在启动的应用程序中正常工作(而不是在设计时模式下)。

ViewModelLocator:

static ViewModelLocator()
    {
        ServiceLocator.Initialize();

        if (ViewModelBase.IsInDesignModeStatic)
        {
            using (var module = new DesignBindingsModule())
            {
                ServiceLocator.Load(module);
            }
        }
        else
        {
            using (var module = new DefaultBindingsModule())
            {
                ServiceLocator.Load(module);
            }
        }
    }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
        "CA1822:MarkMembersAsStatic",
        Justification = "This non-static member is needed for data binding purposes.")]
    public IMainWindowViewModel MainWindow
    {
        get
        {
            return ServiceLocator.Get<IMainWindowViewModel>();
        }
    }

是否可以将 ViewModelLocator 放在单独的程序集中并在设计时模式下绑定到它?

【问题讨论】:

  • 当设计师遇到问题时,我可能会做的一件事是抛出异常以查看正在执行的代码。例如,您可能希望查看ServiceLocator.Get&lt;IMainWindowViewModel&gt;(); 的返回值。 throw ServiceLocator.Get().ToString() 什么的..
  • 您应该将其作为答案提交并接受或删除..

标签: c# .net wpf mvvm-light


【解决方案1】:

问题解决了。我在代码中有一个错误。 ServiceLocator被初始化两次,并抛出异常,无法正确绑定。奇怪的是它只出现在设计器中,而设计器没有显示异常。我重构了代码,问题就消失了。我在单独的程序集中拥有所有 ViewModel 相关类。

【讨论】:

    猜你喜欢
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    相关资源
    最近更新 更多