我想用ServiceLocator
为我的viewmodel构造函数注入一个列表我的viewmodel:
public class ShowEmployeeViewModel: ViewModelBase
{
private IList<IEmployee> _empl;
public ShowEmployeeViewModel(IList<IEmployee> emp)
{
this._empl = emp;
_empl.Add(new Employee() { empName = "foo", enpFunction = "bar" });
}
}
我的服务员:
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
//register the interface against the class
SimpleIoc.Default.Register < IList < IEmployee >, List <Employee>>();
SimpleIoc.Default.Register<ShowEmployeeViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public ShowEmployeeViewModel ShowEmployee
{
get
{
return ServiceLocator.Current.GetInstance<ShowEmployeeViewModel>();
}
}
当我运行此代码时出现错误: “无法注册:在List`1中找到多个构造函数,但没有使用PreferredConstructor标记。” PS:当我尝试注册列表“IList”时,我只收到此错误,但是当我注册这样的界面时:
SimpleIoc.Default.Register < IEmployee , Employee >();
它工作正常,任何想法如何注册列表? 提前谢谢
答案 0 :(得分:1)
请勿映射IList
界面,请为ShowEmployeeViewModel
课程使用工厂:
SimpleIoc.Default.Register(() => new ShowEmployeeViewModel(new List<IEmployee>()));