如何使用MEF容器在ViewModel中注入IRegionManager。我必须在ViewModel的Command委托中切换视图。以下是我正在做的事情的简要描述。我有一个名为Product的实体,其列表显示在一个View(ProductListView)中。在该视图中,用户可以选择产品并点击编辑按钮。这将切换视图并呈现一个新的视图(ProductEditView)。对于激活了不同的看法,我需要这样的棱镜 - 如何使用MEF在ViewModel中导入IRegionManager使用MEF
public class ProductListVM : NotificationObject { //The Product List View Model
[Import]
public IRegionManager RegionManager { get; set; }
private void EditProduct() { //EditCommand fired from ProductListView
IRegion mainContentRegion = RegionManager.Regions["MainRegion"];
//Switch the View in "MainContent" region.
....
}
}
上面的代码失败的NullReferenceException为RegionManager到IRegionManager一些参考。这似乎是合乎逻辑的,因为上述视图模型是由WPF通过Xaml中的DataContext属性构造的,而DI没有发挥作用,所以它没有机会导入RegionManager实例。在这种情况下我们如何解决IRegionManager。