2017-09-12 104 views
1

我剛剛開始使用WPF和Unity從實現MVVM體系結構的偉大源代碼構建應用程序。我跟着它幾乎相同,並審查了他們使用的源代碼,一切都幾乎相同。我創建了一個類ContainerHelper類:Unity CS0308錯誤

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

@ MM8我剛剛注意到,我沒有添加該指令的統一。非常感謝你的幫助。我不明白爲什麼VS不能解決這個問題。 VS知道我正在使用試圖訪問IUnity類,但從來沒有建議添加指令。非常感謝。 –

回答

1

在您所呼叫的通用Resolve方法的代碼文件的頂部添加以下using指令:

using Microsoft.Practices.Unity; 
+0

這是一個教訓,以確保所有使用語句都添加到文件中。我們並不總是看到這些類型錯誤的提示。 –