Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism EventAggregator subscribe executing twice

Tags:

c#

prism

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?

like image 969
Gaz83 Avatar asked Jan 26 '26 17:01

Gaz83


1 Answers

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.

like image 126
Fabio Salvalai Avatar answered Jan 28 '26 12:01

Fabio Salvalai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!