Unity / Caliburn Micro,具有多个参数的注射构造器 [英] Unity/Caliburn Micro, Injection Constructor with multiple parameters

查看:72
本文介绍了Unity / Caliburn Micro,具有多个参数的注射构造器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试学习如何使用Unity和Caliburn Micro实现MVVM。在其他地方寻求帮助后,我仍然不确定如何正确设置构造函数注入。我不知道是否由于我对MVVM的专业知识不足或其他原因而无法正常工作。

I am currently trying to learn how to implement MVVM using Unity and Caliburn Micro. After looking around for help elsewhere I am still unsure about how to set up the Constructor Injection properly. I don't know if this is not working due to my lack of expertise in MVVM or something else.

我的问题是我想将两个IScreen对象传递给我主窗口(外壳)在用户单击按钮之间可以导航的类。这是MainWindowViewModel类中构造函数的代码:

My problem is I want to pass in two IScreen Objects into my main window(shell) Class that can be navigated between when the user clicks on a button. Here is the code for the constructor in my MainWindowViewModel class:

private IScreen campaignViewModel, stringsViewModel;
public MainWindowViewModel(IScreen campaignViewModel, IScreen stringsViewModel)
{
    this.campaignViewModel = campaignViewModel;
    this.stringsViewModel = stringsViewModel;
    ActiveItem = this.campaignViewModel;
}

这是我在Bootstrapper(Unity)类中使用的代码:

This is the code I am using in my Bootstrapper(Unity) class:

    private static IUnityContainer BuildContainer()
    {
        IUnityContainer result = new UnityContainer();
        result
            .RegisterInstance(result)
            .RegisterInstance<IWindowManager>(new WindowManager())
            .RegisterInstance<IEventAggregator>(new EventAggregator());

        result
            .RegisterType<IScreen, CampaignsViewModel>()
            .RegisterType<IScreen, StringsViewModel>()
            .RegisterType<MainWindowViewModel>(new InjectionConstructor(typeof(IScreen), typeof(IScreen)));

        return result;
    }

    protected override object GetInstance(Type service, string key)
    {
        var result = unity.Resolve(service, key);

        if (result == null)
            throw new Exception(string.Format("Could not locate any instance of contract {0}", service));
        return result;
    }

在运行MainWindowViewModel时,它会收到两个StringsViewModel实例。尽管如果要在语句中放入键:

When this runs the MainWindowViewModel receives two instances of StringsViewModel. Although if I were to put a key into the statement:

result.RegisterType<IScreen, StringsViewModel>("StringsView");

然后,它在CampaignsViewModel的两个实例中传递。我不知道如何指定我要让它传递给CampaignViewModel和StringsViewModel的一个实例的InjectionConstructor。我感觉它可能与GetInstance方法有关,但是我不确定。

Then it passes in two instances of CampaignsViewModel. I don't know how to specify to the InjectionConstructor that I want it to pass in one instance of CampaignViewModel and StringsViewModel. I have a feeling it may have something to do with the GetInstance method, but I am unsure.

任何帮助将不胜感激!

谢谢。

推荐答案

在构造函数中,您两次定义了相同的接口。

In your constructor you define the same interface twice.

DI框架如何知道哪个?答案:不能。

How will the DI framework know which is which?? Answer: it can't.


因此,请不要使用2个接口:

So instead of this use 2 interfaces:



 public MainWindowViewModel(ICampaignScreen campaignViewModel, IStringsScreen stringsViewModel)

您可以使这两个继承自IScreen:

You can make both of these inherit from IScreen:

 public interface ICampaignScreen : IScreen

也不需要向IScreen添加任何内容-它们只是为框架提供了一种区分它们的方法。

and neither need add anything to IScreen - they simply provide a means for the Framework to differentiate between them.

这篇关于Unity / Caliburn Micro,具有多个参数的注射构造器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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