An easy to use event aggregator. Thread safe, uses only weak references
$ dotnet add package WeakEventAggregatorThe WeakEventAggregatoris an easy to use EventAggregator providing following benefits
Event types are all classes that implements the (empty) IEvent interface
EventArgs (Payloads) are all classes that implements the (empty) IPayload interface
Creating an instance
IEventAggregator eventAggregator = Factory.GetNewEventAggregatorInstance("myEvAgg", false);Using the following delegate/method
private static void ShowMessage(MsgEventArgs eventArgs)
{
MessageBox.Show(eventArgs.Message, eventArgs.Title);
}Subscribing an event 'MsgEvent' and event args (payload) of type MsgEventArgs
eventAggregator.Subscribe<MsgEvent, MsgEventArgs>(ShowMessage);Raising an event
eventAggregator.Publish<MsgEvent, MsgEventArgs>(new MsgEventArgs { Message = "Test" });Unsubscribing an event 'MsgEvent'
eventAggregator.Unsubscribe<MsgEvent, MsgEventArgs>(ShowMessage);This example uses this event
public class MsgEvent : IEvent
{
}and this event args (payload)
public class MsgEventArgs : IPayload
{
public string Title { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
}