PRISM IEventAggregator.GetEvent<>()deos不返回正确的事件

时间:2017-01-29 16:23:08

标签: wpf mvvm prism-4

我想让EventAggregator在我的PRISM应用程序中运行。我知道有几个教程,指南和讨论,但似乎没有人可以解决我遇到的问题。这是我目前的设置。

我有几个模块,每个模块都有打开新模态窗口的功能。我需要从模块A打开模块B的模态窗口。看起来很简单。我试图使用IEventAggregator从模块B触发事件并在模块A中捕获它,这是行不通的。经过几个小时的研究后,我决定使用Shell来订阅所有事件,因为这样可以消除订阅者在触发事件之前收集垃圾的可能性。如果Shell是订户,则无法进行GC,因为它始终处于活动状态,对吧?是的,但这不行。这是一些代码:

事件本身(注意使用PRISM 4.1):

public class OpenNewPurchaseWindowEvent : CompositePresentationEvent<Customer>
{
}

在壳牌中订阅如下:

    public ShellView(IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        InitializeComponent();
        var newPurchaseEvent = this.eventAggregator.GetEvent<PRISM.DbApp.Shared.OpenNewPurchaseWindowEvent>();
        this.openNewPurchaseWindowEventToken = newPurchaseEvent.Subscribe(OpenNewPurchase);
    }

尝试从模块B中解雇事件:

        var eventt = this.eventAggregator.GetEvent<PRISM.DbApp.Shared.OpenNewPurchaseWindowEvent>();
        eventt.Publish(vm.SelectedCustomer);

在这个模块的构造函数中,我得到了IEventAggregator:

public CustomersView(IEventAggregator eventAggregator)
    {
        this.eventAggregator = eventAggregator;
        InitializeComponent();
        this.DataContext = vm = new CustomersViewModel();
    }

理论上这应该是有效的,但事实并非如此,我无法理解为什么。这里也有一些有趣的东西。

事件订阅进展顺利但是当我尝试在发布事件之前触发事件并检查IEventAggregator时,我得到了这个:

0 Subscriptors

似乎有两个事件,其中一个有1个用户,另一个没有。有趣的是,GetEvent&lt;&gt;()返回一个有0个订阅者,因此发布它没有效果。

任何人都可以解释为什么会这样吗?关于我试图实施的方法有什么建议吗?有没有更好的方法?

0 个答案:

没有答案