I ended up using github.com/shiftkey/Reactive.EventAggregator
as I already use Reactive in my program and it is quite simple
In MainWindow I have
using Reactive.EventAggregator;
MyEventAggregator.MessageAggregator = new EventAggregator();
MyEventAggregator.MessageAggregator.GetEvent<MessageEvent>().Subscribe(se =>
myMessageControl.Add(se)
);
Then there is a UserControl that has a ListBox LB
using Reactive.EventAggregator;
public partial class MessageControl : UserControl
{
public MessageControl()
{
InitializeComponent();
LB.SetBinding(ListBox.ItemsSourceProperty, new Binding { Source = this.MessageCollection });
}
public ObservableCollection<MessageEvent> MessageCollection = new ObservableCollection<MessageEvent>();
public void Add(MessageEvent m)
{
MessageCollection.Add(m);
}
}
public class MyEventAggregator
{
public static EventAggregator MessageAggregator { get; set; }
public static void PostMessage(string message)
{
MessageAggregator.Publish(new MessageEvent(message));
}
}
From anywhere in program I can use
MyEventAggregator.PostMessage("This message appears in MessageControl's Listbox");
It can be even simplified but I want the MessageEvent to hold more than just a string in the future.
Memory Leaksare you writing code where you are not either implementing theAuto Disposing of Objects i. using(){}or not cleaning up objects yourself..You need to implement the INotifyPropertyChangedif you are not familiar with how to do that then here is where you can start readingDanielINotifyPropertyChange-=ing event handlers can pin otherwise properly disposed objects in the heap and prevent the GC from cleaning them up. That's not to say that there isn't an easy solution to the problem (ie : removing handlers that you've added), but there are more ways to provoke a memory leak than you've suggested.JI am well aware of the many ways that Memory Leaks can be Introduced I was just asking in general