MVVM&&;国际奥委会&;子视图模型
我有一个ViewModel,它在构造函数中使用两个相同类型的参数:MVVM&&;国际奥委会&;子视图模型,mvvm,inversion-of-control,ninject,Mvvm,Inversion Of Control,Ninject,我有一个ViewModel,它在构造函数中使用两个相同类型的参数: public class CustomerComparerViewModel { public CustomerComparerViewModel(CustomerViewModel customerViewModel1, CustomerViewModel customerViewModel2) { } } public cl
public class CustomerComparerViewModel
{
public CustomerComparerViewModel(CustomerViewModel customerViewModel1,
CustomerViewModel customerViewModel2)
{
}
}
public class CustomerViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
如果我没有使用IOC,我就可以新建viewmodel并将子viewmodels传入。我可以将两个viewmodel打包成一个类并将其传递到构造函数中,但是如果我有另一个viewmodel只需要一个CustomerServiceWModel,我需要传递viewmodel不需要的东西
我如何利用国际奥委会来处理这个问题?顺便说一句,我用的是Ninject
谢谢我对Ninject不熟悉,但在我看来,为了让IoC知道要将什么CustomerServiceModels注入到构造函数中,您必须提前设置这些对象。使用类似MEF的属性和Psuedo代码,它可能看起来像
[Export()]
public class CustomerSelectorViewModel
{
[Export("CustomerA")]
public class CustomerViewModel FirstSelection {get;set;}
[Export("CustomerB")]
public class CustomerViewModel SecondSelection {get;set;}
}
[Export()]
public class CustomerComparerViewModel
{
[ImportingConstructor]
public CustomerComparerViewModel([Import("CustomerA")]CustomerViewModel customerViewModel1, [Import("CustomerB")]CustomerViewModel customerViewModel2)
{
}
}
以下是如何在Ninject中执行此操作:
Container.Bind<CustomerViewModel>().ToSelf().WhenTargetHas<CustomerA>();
Container.Bind<CustomerViewModel>().ToSelf().WhenTargetHas<CustomerB>();
public class CustomerComparerViewModel
{
public CustomerComparerViewModel([CustomerA]CustomerViewModel customerA,
[CustomerB]CustomerViewModel customerB)
{
}
}