3

I have these throughout my code. It's a WP7 Silverlight app.

UIThreadExecutor.UIThreadExec.Execute(() => buttonControl.Click += 
                                new RoutedEventHandler(this.ButtonClickHandler));

So, the above code, on the UI thread assigning buttonControl.Click event to the event handler ButtonClickHandler .. eg:

public void ButtonClickHandler(object sender, System.Windows.RoutedEventArgs e)
{

}

What I'd like is refactor the:

UIThreadExecutor.UIThreadExec.Execute(() => buttonControl.Click += 
                                new RoutedEventHandler(this.ButtonClickHandler));

into a single static but generic helper method - capable for specifying any UI control event and an event handler. Then the method will hook the two together using the UIThreadExecutor class.

Of course, buttonControl could also be any UI control - with different events of the same type. Eg - it could be a RadioButton with a Checked event.

If I goto the definition in VS 2010 of a RadioButton.Checked or Button.Click they are both of the same type:

public event RoutedEventHandler Checked;

I've been scratching my head about this. I thought about, inside my static helper - declaring a delegate (declared at the namespace level):

public delegate void UIControlHandler(object sender, RoutedEventArgs e);

Then my helper method looks like this:

public static void SubscribeToUIEvent(EventHandler eventToSubscribeTo, 
                                                        UIControlHandler handler)
{
    UIThreadExecutor.UIThreadExec.Execute(() => eventToSubscribeTo += handler);
}

That comes up with compilation errors:

Operator '+=' cannot be applied to operands of type 'System.EventHandler' and UIControlHandler
Cannot implicitly convert type UIControlHandler' to 'System.EventHandler'

Can anyone help point me in the right direction? This is driving me crazy.

TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • 2
    Just get rid of this UIThreadExecutor altogether, subscribing events can be done on a worker thread. Although doing so is *very* unusual, hard to see why you need this. – Hans Passant Nov 05 '11 at 18:55
  • This is not quite right - `assigning buttonControl.Click event to the event handler ButtonClickHandler`, Event Handler assigned to Event not the opposite – sll Nov 05 '11 at 19:10

1 Answers1

2

Keyword: MulticastDelegate

Here is a general overview about events/delegates in C#. http://www.codeproject.com/KB/cs/delegates_overview.aspx

Of course you can use interfaces with event declerations too.

Edit 2:

I found this: How to pass an event to a method? This should help, i don't think you'll get a better solution, because it's not possible to pass ref params to a anonymus method.

Community
  • 1
  • 1
Felix K.
  • 6,201
  • 2
  • 38
  • 71
  • Thank you for the replies and the link. I think I understand MulticastDelegate. Where you can assign multiple methods to a single delegate. Each method gets executed when you invoke the single delegate. But I'm not sure how I can use that to having generic method that takes as parameters - an object's event and a event handler method. I then have to use that UIThreadExecutor class - to hook them together. That UIThreadExecutor class was provided to me and I must use it. Would be very grateful guys, if you could point me in the right direction. Thanks very much. – Christian Clarke Nov 05 '11 at 22:32
  • Hi @Felix K. Thank you for your post. Unfortunately, I get a compilation error: Cannot use ref or out parameter 'eventToSubscribeTo' inside an anonymous method, lambda expression, or query expression. – Christian Clarke Nov 06 '11 at 09:35
  • Updated it, thats the best solution i can offer. – Felix K. Nov 06 '11 at 10:11
  • Thank you for the help & link Felix K. I'm most grateful! – Christian Clarke Nov 06 '11 at 13:28