我在WPF中使用Prism。当我将IEventAggregator作为参数添加到ViewModel构造函数时,我收到此错误: Microsoft.Practices.ServiceLocation.dll中发生类型“Microsoft.Practices.ServiceLocation.ActivationException”的异常。 其他信息:尝试获取Object类型的实例时出现激活错误,键“CategoriesView”
此行触发了异常:
private void NavigateToCategoriesRadioButton_Click(object sender, RoutedEventArgs e)
{
this.regionManager.RequestNavigate(RegionNames.ConfigurationContentRegion, categoriesViewUri);
}
其中categoriesViewUri是:
private static Uri categoriesViewUri = new Uri("/CategoriesView", UriKind.Relative);
这是我的视图模型类:
[Export]
public class CategoriesViewModel : BindableBase
{
private readonly IRegionManager regionManager;
private readonly IEventAggregator eventAggregator;
private readonly IConfigurationCategoriesService categoriesService;
private readonly ObservableCollection<Category> categoriesCollection;
private readonly ICollectionView categoriesView;
private readonly DelegateCommand<object> deleteCategoryCommand;
[ImportingConstructor]
public CategoriesViewModel(IEventAggregator eventAggregator, IConfigurationCategoriesService categoriesService, IRegionManager regionManager)
{
this.categoriesService = categoriesService;
this.regionManager = regionManager;
this.eventAggregator = eventAggregator;
this.deleteCategoryCommand = new DelegateCommand<object>(this.DeleteCategory, this.CanDeleteCategory);
this.categoriesCollection = new ObservableCollection<Category>(categoriesService.GetCategories());
this.categoriesView = new ListCollectionView(this.categoriesCollection);
this.categoriesView.CurrentChanged += (s, e) => this.deleteCategoryCommand.RaiseCanExecuteChanged();
}
public ICollectionView Categories
{
get { return this.categoriesView; }
}
public ICommand DeleteCategoryCommand
{
get { return this.deleteCategoryCommand; }
}
private void DeleteCategory(object ignored)
{
var category = this.categoriesView.CurrentItem as Category;
if (category != null)
{
categoriesService.DeleteCategory(category);
}
}
private bool CanDeleteCategory(object ignored)
{
return true;
}
}
看起来CatagoriesViewModel无法在构造函数上获取IEventAggregator的实例,但这是由Prism自动完成的,不是吗?在我从Prism Documentation(StockTraderRI_Desktop)获得的示例中,我没有看到EventAggregator被实例化的任何地方。任何人都可以看到我错了什么?提前致谢
Editted:
Navitagion项目视图在CategoriesModule类中注册:
[ModuleExport(typeof(CategoriesModule))]
public class CategoriesModule : IModule
{
[Import]
public IRegionManager regionManager;
public void Initialize()
{
this.regionManager.RegisterViewWithRegion(RegionNames.ConfigurationNavigationRegion, typeof(CategoriesNavigationItemView));
}
}
和CategoriesView代码隐藏是:
[Export("CategoriesView")]
public partial class CategoriesView : UserControl
{
public CategoriesView()
{
InitializeComponent();
}
[Import]
public IRegionManager regionManager;
[Import]
public CategoriesViewModel ViewModel
{
get { return this.DataContext as CategoriesViewModel; }
set { this.DataContext = value; }
}
}
答案 0 :(得分:0)
我解决了这个问题,添加了以下using语句:
using Microsoft.Practices.Prism.PubSubEvents;
而不是
using Prism.Events;
我也通过本网站的推荐切换到Unity而不是MEF。