SimpleIoc (MVVMlight)依赖注入一个接口列表c# Windows Store

本文关键字:列表 接口 Windows 一个 Store MVVMlight 依赖 注入 SimpleIoc | 更新日期: 2023-09-27 17:53:14

是否可以在接口列表上使用MVVM光中的依赖注入?

我试过让依赖是List<IMyInterface> IList<IMyInterface>。从ViewModelLocator内,我也尝试了有和没有List<>。如果我没有List<>,我得到一个缓存没有列表异常的值,如果我这样做,(对于列表)我得到一个没有首选构造函数异常(因为列表有多个构造函数,我不能设置属性,因为它是。net内部的一个类)

我能想到的唯一可能的解决方案将限制我的可测试性,这将是将所有列表作为具体实现,即我有

List<dataType> data = new List<dataType>();

是否有办法IOC一个列表?还是说你应该写具体的代码?

SimpleIoc (MVVMlight)依赖注入一个接口列表c# Windows Store

ViewModelLocator可以拥有可通过它访问的静态对象。

public class ViewModelLocator
{
    ....
    private static List<IMyInterface> _myInterfaces;
    public static List<IMyInterface> MyInterfaces
    {
        get
        {
            return _myInterfaces;
        }
        set
        {
            // So that it will be readonly. Technically unnecessary, but may be good
            // practice.
            if(_myInterfaces != null) return;
            _myInterfaces = value;
        }
    }
}

然后在你的主应用程序中无论你在哪里得到你的列表

ViewModelLocator.MyInterfaces = GetMyInterfaceList();

希望这有助于和快乐编码!