这是情况
我有 2 个单例类(实际上是 ViewModels)。
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class CompanyViewModel, INavigationAware
{
private Model.Company _selected;
[ImportingConstructor]
public CompanyViewModel(Service.ICompany companyService)
{
Companies = companyService.Companies;
}
[Export("SelectedCompany")]
public Model.Company Selected
{
get
{
return _selected;
}
set
{
_selected = value;
}
}
public ObservableCollection<Model.Company> Companies{get;set;}
public void OnNavigatedTo(NavigationContext navigationContext)
{
_selected = Companies.First();
}
~~~
}
companyService 返回 2 个公司“A”和“B”。导航到 CompanyViewModel 类时,“A”被设置为选定的公司。然后用户选择公司“B”,CompanyViewModel 通过它的绑定更新。
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class DepartmentViewModel, INavigationAware
{
[Import("SelectedCompany")]
private Model.Company _selectedCompany{get;set;};
[ImportingConstructor]
public DepartmentViewModel(Service.IDeparment departmentService)
{
Departments= departmentService.Departments;
}
public ObservableCollection<Model.Department> Departments{get;set;}
public void OnNavigatedTo(NavigationContext navigationContext)
{
this.departmentService.Refresh(_selectedCompany);
}
~~~
}
在上述事件之后,DepartmentViewModel 被创建。问题是 DepartmentViewModel 中的公司属性设置为公司“A”而不是公司“B”,正如我所料,因为我认为 MEF 会导入最后一个值 Selected 设置为(在创建 DepartmentViewModel 之前)而不是第一个值它被设置为。
有人能告诉我到底发生了什么吗?