卡利微构造器注入失败 [英] Caliburn Micro Constructor Injection Failed

查看:301
本文介绍了卡利微构造器注入失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习卡利微并尽量使从的 EventAggregator 的在%20Event%20Aggregator>官方网站



不过,我得到了一个异常




这个定义无参数的构造函数对象。




消息本身是透明的,但这个例子不包括参数的构造函数无论是。如果我添加一个,带参数的构造函数是打不和 IEventAggregator 仍无法正常注入。



下面是我的出版商VM添加参数构造函数(没有它,会抛出异常)后:

 公共MainViewModel(){} 

酒店的公共MainViewModel(IEventAggregator EA):这个()
{
eventAggregator = EA;
}

这是我使用引导程序:

 公共类AppBootstrapper:引导程序< MainViewModel> 
{
私人只读SimpleContainer集装箱=新SimpleContainer();

保护覆盖无效配置()
{
container.Singleton< IEventAggregator,EventAggregator>();
}
}

下面是CM的例子:

  //创建EventAggregator作为一个单身。 
公共类引导程序:BootstrapperBase {
私人只读SimpleContainer _container =
新SimpleContainer();

// ...其他引导程序配置

保护覆盖无效配置(){
_container.Singleton< IEventAggregator,EventAggregator>();
}

// ...其他引导程序配置
}

//获取一个视图模型的EventAggregator。
公共类FooViewModel {
私人只读IEventAggregator _eventAggregator;

公共FooViewModel(IEventAggregator eventAggregator){
_eventAggregator = eventAggregator;
}
}



我检查这个职位(的注入EventAggregator到视图模型与卡利微),但它只是没有说什么,为什么CM不调用构造函数注入。



我也查了CM的样品溶液,但它使用MEF作为DI解决方案。



我怎么错过?


解决方案

  // ...其他引导程序配置

这是其他引导程序的配置是非常重要的。



最好的办法是通过 Caliburn.Micro.Start 的NuGet包安装Caliburn.Micro,并有。看看提供的引导程序也使用由Caliburn.Micro提供的 SimpleContainer



这是完全:

 公共类AppBootstrapper:BootstrapperBase 
{
SimpleContainer容器;

公共AppBootstrapper()
{
开始();
}

保护覆盖无效配置()
{
容器=新SimpleContainer();
container.Singleton< IWindowManager,窗口管理器>();
container.Singleton< IEventAggregator,EventAggregator>();
container.PerRequest< IShell的,ShellViewModel>();
}

保护覆盖对象的GetInstance(服务类型,字符串键)
{
无功实例= container.GetInstance(服务密钥);
如果(比如!= NULL)
返回实例;
抛出新的InvalidOperationException异常(无法找到任何实例。);
}

保护覆盖的IEnumerable<对象> GetAllInstances(服务类型)
{
返回container.GetAllInstances(服务);
}

保护覆盖无效堆积(对象实例)
{
container.BuildUp(实例);
}

保护覆盖无效OnStartup(对象发件人,System.Windows.StartupEventArgs E)
{
DisplayRootViewFor<&IShell的GT;();
}
}


I am learning Caliburn Micro and try to make use of the EventAggregator from the official site.

However, I got an exception

"No parameterless constructor defined for this object."

The message itself is clear but the example doesn't include a parameterless constructor either. If i add one, the constructor with parameter is not hit and the IEventAggregator is still not injected correctly.

Here is my publisher VM after adding the parameterless constructor (without it, exception will be thrown):

    public MainViewModel() { }

    public MainViewModel(IEventAggregator ea) : this()
    {
        eventAggregator = ea;
    }

And here is the bootstrapper i am using:

public class AppBootstrapper : Bootstrapper<MainViewModel>
{
    private readonly SimpleContainer container = new SimpleContainer();

    protected override void Configure()
    {
        container.Singleton<IEventAggregator, EventAggregator>();
    }
}

Here is the example from CM:

// Creating the EventAggregator as a singleton.
public class Bootstrapper : BootstrapperBase {
    private readonly SimpleContainer _container =
        new SimpleContainer();

     // ... Other Bootstrapper Config

    protected override void Configure(){
        _container.Singleton<IEventAggregator, EventAggregator>();
    }

    // ... Other Bootstrapper Config
}

// Acquiring the EventAggregator in a viewModel.
public class FooViewModel {
    private readonly IEventAggregator _eventAggregator;

    public FooViewModel(IEventAggregator eventAggregator) {
        _eventAggregator = eventAggregator;
    }
}

I checked this post (Inject EventAggregator into ViewModel with Caliburn Micro) but it simply said nothing why the CM do not invoke the constructor with injection.

I also checked the CM's sample solution but it uses MEF as the DI solution.

What do I miss?

解决方案

// ... Other Bootstrapper Config

It's the other bootstrapper config that's important.

The best option is to install Caliburn.Micro via the Caliburn.Micro.Start NuGet package, and have a look at the provided bootstrapper which also uses the SimpleContainer provided by Caliburn.Micro.

Here it is in full:

public class AppBootstrapper : BootstrapperBase
{
     SimpleContainer container;

     public AppBootstrapper()
     {
         Start();
     }

     protected override void Configure()
     {  
         container = new SimpleContainer();
         container.Singleton<IWindowManager, WindowManager>();
         container.Singleton<IEventAggregator, EventAggregator>();
         container.PerRequest<IShell, ShellViewModel>();
     }

     protected override object GetInstance(Type service, string key)
     {
         var instance = container.GetInstance(service, key);
         if (instance != null)
             return instance;
         throw new InvalidOperationException("Could not locate any instances.");
     }

     protected override IEnumerable<object> GetAllInstances(Type service)
     {
         return container.GetAllInstances(service);
     }

     protected override void BuildUp(object instance)
     {
         container.BuildUp(instance);
     }

     protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
     {
         DisplayRootViewFor<IShell>();
     }
}

这篇关于卡利微构造器注入失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆