2016-11-20 75 views
0

我在我的WPF中使用棱鏡。當我將IEventAggregator作爲參數添加到ViewModel構造函數時,出現以下錯誤:Microsoft.Practices.ServiceLocation.dll中發生了「Microsoft.Practices.ServiceLocation.ActivationException」類型的異常。IEventAggregator棱鏡激活錯誤

private void NavigateToCategoriesRadioButton_Click(object sender, RoutedEventArgs e) 
{ 
    this.regionManager.RequestNavigate(RegionNames.ConfigurationContentRegion, categoriesViewUri); 
} 

其中categoriesViewUri是:

private static Uri categoriesViewUri = new Uri("/CategoriesView", UriKind.Relative); 
嘗試獲取類型對象的實例,關鍵 「CategoriesView」

異常觸發在這條線上發生激活錯誤: 更多信息

這是我的觀點模型類:

[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類registerd:

[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

有關內部例外的更多信息?順便說一句 - 這裏有一個沒有參數的委託命令,在這裏不需要一個未使用的對象。 – Haukinger

+0

你怎麼知道問題出在IEventAggregator上?它不是源於錯誤消息,我從來沒有遇到任何導入它的問題。更有可能的罪魁禍首是你沒有'IConfigurationCategoriesService'的任何輸出。此外,該錯誤認爲'CategoriesView'而不是'CategoriesViewModel',那麼你怎麼知道問題出在那裏? – Grx70

+0

Hi @ Grx70。我會說問題出在那裏,因爲如果我從CategoriesViewModel的構造函數中刪除IEventAggregator,它可以正常工作。 – chincheta73

回答

0

我解決了這個問題加入以下使用說明:

using Microsoft.Practices.Prism.PubSubEvents; 

代替

using Prism.Events; 

我也切換到Unity,而不是通過MEF推薦這個網站。