Pavel Pavlov
Pavel Pavlov

Reputation: 767

PRISM IEventAggregator.GetEvent<>() deos not return the correct event

I am trying to get EventAggregator to work in my PRISM app. I know there are several tutorials and guides and discussions, but it seems non of them can address the issue I encounter. Here is my current setup.

I have several modules and each module has the functionality to open a new modal window. I need to open the modal window of module B from module A. Seems pretty straight-forward. I am attempting to use an IEventAggregator to fire an event from module B and catch it in module A and this is not working. After several hours of researching I decided to use the Shell to subscribe to all events because this would eliminate the possibility that the subscriber get garbage collected before the event is triggered. If the Shell is the subscriber it cannot be GCed because it is always active, right? Yes, but this is not working as well. Here is some code:

The event itself (note using PRISM 4.1):

public class OpenNewPurchaseWindowEvent : CompositePresentationEvent<Customer>
{
}

Subscribing in the Shell like this:

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

And trying to fire the event from Module B:

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

In the constructor of this module I get the IEventAggregator:

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

In theory this should be working, but it is not and I cannot understand why. Here is something interesting as well.

Event subscription goes well but when I try to trigger the event and inspect the IEventAggregator just before publishing the event I get this:

0 Subscriptors

It seems like there are two events and one of them has 1 subscriber the other does not. Interestingly the GetEvent<>() returns the one that has 0 Subscribers thus publishing it has no effect.

Can anyone explain why this is happening? Are there any suggestions regarding the approach I am trying to implement? Is there any better approach?

Upvotes: 0

Views: 229

Answers (0)

Related Questions