1

I'm trying to use the eventaggregator (Unity container) to fire/publish an event when my shell is closing so I can save some settings in a View. The problem is when I exit the application there is no longer any subscribers to the event, it's as if something is clearing them out. I'm new to event aggregation so apologies if this is something obvious!

I've done this in my Shell:

    public Shell(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        InitializeComponent();
    }

    protected override void OnClosing(CancelEventArgs e)
    {

        var theevent =_eventAggregator.GetEvent<ShellClosingEvent>();
        theevent.Publish("closing");

    }

And my View:(I subscribe in the constructor)

        eventAggregator.GetEvent<ShellClosingEvent>().Subscribe((x) =>
        {
            if (x != "closing") return;
            using (var fs = new FileStream("clientGridSettings.xml", FileMode.Create, FileAccess.Write))
            {
                ClientsGrid.SaveCustomizations(fs);
            }

        });

The event:

public class ShellClosingEvent : PubSubEvent<string>
{
}
Michael Harper
  • 1,531
  • 2
  • 25
  • 42
  • Did you try SubscriptionToken Subscribe(Action action, bool keepSubscriberReferenceAlive) – quadroid Jul 16 '14 at 10:34
  • possible duplicate of [Prism Event Aggregation - subscriber not triggered](http://stackoverflow.com/questions/1810730/prism-event-aggregation-subscriber-not-triggered) or http://stackoverflow.com/questions/1132690/composite-wpf-eventaggregator-subscriptions-being-lost/1132775#1132775 ? – stijn Jul 16 '14 at 10:39
  • Hi, I tried eventAggregator.GetEvent().Subscribe(OnClosing, true); But the event aggregator still has no subscribers when my Shell OnClosing is fired :( – Michael Harper Jul 16 '14 at 11:17

0 Answers0