我刚刚开始使用WPF和Unity构建应用程序,这是一个实现MVVM架构的优秀来源。我跟着它几乎相同,并查看了他们使用的源代码,一切都几乎相同。我创建了一个ContainerHelper类:
private static IUnityContainer _container;
static ContainerHelper()
{
_container = new UnityContainer();
_container.RegisterType<IEmployeesRepository, EmployeesRepository>(
new ContainerControlledLifetimeManager());
}
public static IUnityContainer Container
{
get { return _container; }
}
我创建了一个类EmployeeListViewModel:
私有IEmployeesRepository _repo;
public EmployeeListViewModel(IEmployeesRepository repo)
{
_repo = repo;
EditEmployeeCommand = new RelayCommand<Employees>(OnEditEmployee);
AddEmployeeCommand = new RelayCommand(OnAddEmployee);
ClearSearchCommand = new RelayCommand(OnClearSearch);
}
private string _SearchInput;
public string SearchInput
{
get { return _SearchInput; }
set
{
SetProperty(ref _SearchInput, value);
FilterEmployee(_SearchInput);
}
}
private void FilterEmployee(string searchInput)
{
if (string.IsNullOrWhiteSpace(searchInput))
{
Employees = new ObservableCollection<Employees>(_allEmployees);
}
else
{
Employees = new ObservableCollection<Employees>(_allEmployees.Where(e => e.FullName.ToLower().Contains(searchInput.ToLower())));
}
}
private void OnClearSearch()
{
SearchInput = null;
}
private void OnEditEmployee(Employees emp)
{
EditEmployeeRequest(emp);
}
private void OnAddEmployee()
{
AddEmployeeRequested(new Employees { Id = Guid.NewGuid() });
}
public event Action<Employees> AddEmployeeRequested = delegate { };
public event Action<Employees> EditEmployeeRequest = delegate { };
private ObservableCollection<Employees> _Employees;
public ObservableCollection<Employees> Employees
{
get { return _Employees; }
set { SetProperty(ref _Employees, value); }
}
private List<Employees> _allEmployees;
public async void LoadEmplooyees()
{
_allEmployees = await _repo.GetEmployeesAsync();
Employees = new ObservableCollection<Employees>(_allEmployees);
}
public RelayCommand<Employees> EditEmployeeCommand { get; private set; }
public RelayCommand AddEmployeeCommand { get; private set; }
public RelayCommand ClearSearchCommand { get; private set; }
}
这是使用ContainerHelper类实现和传递EmployeeListViewModel的代码行:
private EmployeeListViewModel _EmployeeListViewModel;
_EmployeeListViewModel = ContainerHelper.Container.Resolve<EmployeeListViewModel>();
我不明白为什么我使用IUnityContainer获取非泛型方法错误.Resolve(type,sring,params ResolverOverride [])不能与类型参数一起使用。
我想更多地了解这个错误以及我可以做些什么来解决它...我已经在各个地方寻找答案。
答案 0 :(得分:2)
在您调用通用using
方法的代码文件顶部添加以下Resolve
指令:
using Microsoft.Practices.Unity;