I am using Prism in my WPF. When I add IEventAggregator as a parameter to the ViewModel constructor I get this error: An exception of type 'Microsoft.Practices.ServiceLocation.ActivationException' occurred in Microsoft.Practices.ServiceLocation.dll. Additional information: Activation error occurred while trying to get instance of type Object, key "CategoriesView"
The exception is triggered in this line:
private void NavigateToCategoriesRadioButton_Click(object sender, RoutedEventArgs e)
{
this.regionManager.RequestNavigate(RegionNames.ConfigurationContentRegion, categoriesViewUri);
}
where categoriesViewUri is:
private static Uri categoriesViewUri = new Uri("/CategoriesView", UriKind.Relative);
This is my view model 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;
}
}
It looks like CatagoriesViewModel cannot get an instance of IEventAggregator on the constructor but this is done automatically by Prism, isn't it? In the example I have from Prism Documentation (StockTraderRI_Desktop) I don´t see anywhere where the EventAggregator is instantiated. Can anyone see what am I getting wrong? Thanks in advance
Editted:
The Navitagion item view is registerd in the CategoriesModule class:
[ModuleExport(typeof(CategoriesModule))]
public class CategoriesModule : IModule
{
[Import]
public IRegionManager regionManager;
public void Initialize()
{
this.regionManager.RegisterViewWithRegion(RegionNames.ConfigurationNavigationRegion, typeof(CategoriesNavigationItemView));
}
}
And CategoriesView code-behind is:
[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; }
}
}
I solved this issue adding the following using statement:
using Microsoft.Practices.Prism.PubSubEvents;
instead of
using Prism.Events;
I also switched to Unity instead of MEF by recommendation on this site.