IEventAggregator Prism 激活错误
Activation error with IEventAggregator Prism
我在 WPF 中使用 Prism。当我将 IEventAggregator 作为参数添加到 ViewModel 构造函数时,出现此错误:'Microsoft.Practices.ServiceLocation.ActivationException' 类型的异常发生在 Microsoft.Practices.ServiceLocation.dll 中。
附加信息:尝试获取对象类型的实例时发生激活错误,密钥 "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);
这是我的视图模型class:
[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 文档 (StockTraderRI_Desktop) 获得的示例中,我没有看到实例化 EventAggregator 的任何地方。任何人都可以看到我做错了什么吗?提前致谢
已编辑:
Navitagion 项目视图已在 CategoriesModule 中注册 class:
[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; }
}
}
我通过添加以下 using 语句解决了这个问题:
using Microsoft.Practices.Prism.PubSubEvents;
而不是
using Prism.Events;
我也根据本网站的推荐切换到 Unity 而不是 MEF。
我在 WPF 中使用 Prism。当我将 IEventAggregator 作为参数添加到 ViewModel 构造函数时,出现此错误:'Microsoft.Practices.ServiceLocation.ActivationException' 类型的异常发生在 Microsoft.Practices.ServiceLocation.dll 中。 附加信息:尝试获取对象类型的实例时发生激活错误,密钥 "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);
这是我的视图模型class:
[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 文档 (StockTraderRI_Desktop) 获得的示例中,我没有看到实例化 EventAggregator 的任何地方。任何人都可以看到我做错了什么吗?提前致谢
已编辑:
Navitagion 项目视图已在 CategoriesModule 中注册 class:
[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; }
}
}
我通过添加以下 using 语句解决了这个问题:
using Microsoft.Practices.Prism.PubSubEvents;
而不是
using Prism.Events;
我也根据本网站的推荐切换到 Unity 而不是 MEF。