" IoC未初始化。"在Caliburn Micro中使用DesignTime支持进行级联ViewModel时出错

时间:2016-08-24 14:53:37

标签: c# wpf mvvm caliburn.micro design-time

我已将Bootstrapper放在一个单独的程序集中,而不是ViewModels和Views。 但是,当没有其他需要解析相应视图的Screen时,DesignTime支持似乎适用于简单的* ViewModel-> *视图可视化。但是我不能让它为子ViewModels运行。我得到了一个异常......

具有子ViewModel的容器:

<UserControl ...
    d:DataContext="{d:DesignInstance Type=vm:DesignSomeContainerViewModel, IsDesignTimeCreatable=True}"
    cal:Bind.AtDesignTime="True ... >
  <ContentControl cal:View.Model={Binding PropertyWithChildModel} />
  // where PropertyWithChildModel is another Screen

属性中的屏幕在DesignTime上显示时是正常的,但不在另一个View的ContentControl内...然后DesignTime支持被破坏。我遵循了Caliburn Micro Documentation建议的指导。但是在使用嵌套解析时没有任何关于专业的说法。

我的项目结构:

[Application.exe]
- MyNameSpace.ApplicationName
  - Bootstrapper (knows assemblies with views)
[Satellite.dll]
- MyNameSpace.SomeWhere
  - SomeContainerViewModel
    - [DesignTime]/DesignSomeContainerViewModel (Folder is Namespaceprovider)
  - SomeContainerView (SomeContainerView is set to look at DesignSomeContainerViewModel at DesignTime)
  - [MyChilds]/SomeChildViewModel
    - [DesignTime]/DesignSomeChildViewModel
  - [MyChilds]/SomeChildView (SomeChildView is set to look at DesignSomeChildViewModel)

班级模型:

public interface ISomeChildViewModel
{
    string SomeValue {get;}
}

// should not be used during design time
public class SomeChildViewModel : Screen, ISomeChildViewModel
{
     // real implementation
}

// this is used at design time
public class DesignSomeChildViewModel : Screen, ISomeChildViewModel
{
   public DesignSomeChildViewModel()
   {
       this.SomeValue = "Hello World!";
       // showing up correctly in the Xaml View at DesignTime
   }

    string SomeValue {get;set;}
}

public interface ISomeContainerViewModel
{
    IScreen PropertyWithChildModel{get;}
}

// should not be used during design time
public class SomeContainerViewModel : Screen, ISomeContainerViewModel
{
    // real implementation
}

// used during design time
public class DesignSomeContainerViewModel : Screen, ISomeContainerViewModel
{
   public DesignSomeContainerViewModel ()
   {
       // this assignment leads to an exception that the IoC is not ready
       // "IoC is not initialized."
       this.PropertyWithChildModel = new DesignSomeChildViewModel();
   }

    IScreen PropertyWithChildModel
    {
       get { return propertyWithChildModel; }
       set
       {
            this.propertyWithChildModel=value;
            this.NotifyOfPropertyChange();
       }
    }
}

我已尝试/检查的内容:

  • Bootstrapper:SelectAssembly中的程序集列表包含带有Views的程序集
  • ViewModelLocator:设置为与DesignTime ViewModel而不是ViewModel匹配(即使没有,也应该有一条消息说View不能为ViewModel解析)
  • 使用Conductor with ActiveItem而不是简单属性
  • 容器De​​signSomeContainerViewModel和SomeContainerViewModel与IScreen PropertyWithChild {get;}共享相同的接口,绑定指向正确的属性

0 个答案:

没有答案