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>
{
}