title: Passing an Event and a delegate event handler into a generic Helper Method
body: <p>I have these throughout my code.  It's a WP7 Silverlight app.</p>
  
  <pre><code>UIThreadExecutor.UIThreadExec.Execute(() =&gt; buttonControl.Click += 
                                  new RoutedEventHandler(this.ButtonClickHandler));
  </code></pre>
  
  <p>So, the above code, on the UI thread assigning <code>buttonControl.Click</code> event to the event handler ButtonClickHandler .. eg:</p>
  
  <pre><code>public void ButtonClickHandler(object sender, System.Windows.RoutedEventArgs e)
  {
  
  }
  </code></pre>
  
  <p>What I'd like is refactor the: </p>
  
  <pre><code>UIThreadExecutor.UIThreadExec.Execute(() =&gt; buttonControl.Click += 
                                  new RoutedEventHandler(this.ButtonClickHandler));
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>If I goto the definition in VS 2010 of a RadioButton.Checked or Button.Click they are both of the same type:</p>
  
  <pre><code>public event RoutedEventHandler Checked;
  </code></pre>
  
  <p>I've been scratching my head about this.  I thought about, inside my static helper - declaring a delegate (declared at the namespace level):</p>
  
  <pre><code>public delegate void UIControlHandler(object sender, RoutedEventArgs e);
  </code></pre>
  
  <p>Then my helper method looks like this:</p>
  
  <pre><code>public static void SubscribeToUIEvent(EventHandler eventToSubscribeTo, 
                                                          UIControlHandler handler)
  {
      UIThreadExecutor.UIThreadExec.Execute(() =&gt; eventToSubscribeTo += handler);
  }
  </code></pre>
  
  <p>That comes up with compilation errors:</p>
  
  <blockquote>
    <p>Operator '+=' cannot be applied to operands of type
    'System.EventHandler' and UIControlHandler<br>
    Cannot implicitly convert type UIControlHandler' to 'System.EventHandler'</p>
  </blockquote>
  
  <p>Can anyone help point me in the right direction?  This is driving me crazy.</p>