Prism EventAggregator订阅执行两次

时间:2015-09-28 09:42:59

标签: c# prism

我对此帖Prism CompositePresentationEvent fires twice

有类似的问题

我的问题是Publish只触发一次,但Subscribe中的代码执行了两次。

这是我的发布代码。

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

订阅代码。

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

    LoadingVisibility = Visibility.Collapsed;                
});

我在PublishSubscribe都设置了一个断点,并且可以看到发布只会触发一次,但订阅代码会触发两次。这由两次显示的消息框备份。 我已经搜索了任何其他发布代码,但没有找到。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

确保仅调用Subscribe方法一次。

如果它被调用两次,相同的事件将触发相同的回调,就像完全不同的处理程序对同一事件作出反应一样。