EventHandler Subscription

May 29, 2008 at 3:20 AM
I've tried to implement event handling using the Stoplight sample but the Subscriber collection is never populated. I've put a [SubscribesTo("ShowPersonDetails"] on my MainForm so it can react and load the appropriate user control. The event fires so the Publisher collection is being populated.

I've tried the example of registering the extension in the Program.cs like the sample and also in the configuration.

No luck.

Any ideas or other working examples?

Thanks,
John
May 30, 2008 at 2:36 AM

For the record, I figured out some things:

  1. The SubscribeTo wasn't being picked up because the method was marked as private (due to copy/paste error). Since Unity uses reflection it wasn't seeing the method and thus no subscriptions were registered.
  2. You *MUST* declare your eventArgs parameter as System.EventArgs or else Unity will throw an error when first loading/resolving your classes. On the surface this would seem to mean you can't pass values in the event arguments. However, notice I said declare the parameter that way. When you raise the event you can use the EventArgs class in EventBroker to pass values like this:

    handlers(

    this, new EventArgs<int>(contactId));

    and on the subscription side you can cast the generic System.EventArgs parameter back to the EventBroker.EventArgs type like this:

     

     

     

    [

    SubscribesTo(EventTopics.ShowPersonDetails)]
    public

    void OnShowPersonDetails(object sender, EventArgs e)
    {
        EventBroker.EventArgs<int> eventArgs = (EventBroker.EventArgs<int
    >)e;
        personDetailViewControl.Show((
    int
    )eventArgs.Data);
    }

     

Unfortunately, because the StopLight sample also intermixes standard .NET event handling as well as the Unity EventBroker stuff, it's not that obvious when first reviwing it.

Hope this helps somebody else!

John

 


jwatson3d wrote:
I've tried to implement event handling using the Stoplight sample but the Subscriber collection is never populated. I've put a [SubscribesTo("ShowPersonDetails"] on my MainForm so it can react and load the appropriate user control. The event fires so the Publisher collection is being populated.

<snip.