Subscribe extension method

Subscribes the caller instance to an event with specified ID.

Subscribe extension method signature
public static EventMessageHandler Subscribe(this object eventSource, EventHub eventHub, int id, Action<EventMessage> handler)

Input arguments
  • eventSource is an instance of the object, which provides the event handler
  • eventHub is an object that manages event messaging
  • id is an event message identifier
  • handler is an event message handler
Return value
  • eventMessageHandler is an instance of the EventMessageHandler class

Example

Subscribe MainWindow to EventId.Default.Dispatch event ID and use HandleDispatch method as event message handler.

Get message parameter "Dispatch" and use its value as an Action delegate to invoke a method in the Dispatcher.

using System.Windows;
using EventAggregator;

namespace EventAggregatorSample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Subscribe(EventId.Default.Dispatch, HandleDispatch);
        }

        private void HandleDispatch(EventMessage eventMessage)
        {
            Dispatcher.Invoke(eventMessage.GetValue<Action>("Dispatch"));
        }
    }
}

Last edited Apr 19, 2012 at 6:17 PM by DarkVasya, version 6

Comments

No comments yet.