Gaz83
Gaz83

Reputation: 2865

Prism EventAggregator subscribe executing twice

I have a similar issue to this post Prism CompositePresentationEvent fires twice

My issue is that the Publish is only firing once but the code in the Subscribe is executing twice.

Here is my publish code.

EventAggregator.GetEvent<PubSubEvent<HardwareLoaded>>().Publish(new HardwareLoaded() { HardwareOK = _HardwareOK });

And the subscribe code.

IEventAggregator.GetEvent<PubSubEvent<HardwareLoaded>>().Subscribe(x =>
{
    if (!x.HardwareOK)
    {
        MessageBox.Show("There was an issue loading hardware. See Log");
    }

    LoadingVisibility = Visibility.Collapsed;                
});

I put a breakpoint at both the Publish and the Subscribe and can see that the publish only fires once but the subscribe code fires twice. This is backed up by the message box showing twice. I have run a search for any other publish code and found none.

Any ideas?

Upvotes: 3

Views: 2685

Answers (1)

Fabio Salvalai
Fabio Salvalai

Reputation: 2509

Make sure the Subscribe method is called only once.

If it is called twice, the same event will trigger the same callback, just as it would if there were completely different handlers reacting to the same event.

Upvotes: 8

Related Questions