将事件和委托事件处理程序传递给通用的助手方法 [英] Passing an Event and a delegate event handler into a generic Helper Method

查看:163
本文介绍了将事件和委托事件处理程序传递给通用的助手方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中有这些。这是一个WP7 Silverlight应用程序。

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

所以,上面的代码,在UI线程上分配 buttonControl.Click 事件到事件处理程序ButtonClickHandler ..例如:

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

}

d喜欢重构:

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

成一个静态但通用的助手方法 - 能够指定任何UI控件事件和事件处理程序。然后,该方法将使用UIThreadExecutor类将两者钩在一起。



当然,buttonControl也可以是任何UI控件 - 具有相同类型的不同事件。例如 - 它可以是一个带有Checked事件的RadioButton。



如果我去了一个RadioButton.Checked或Button的VS 2010中的定义,他们都是一样的类型:

 公共事件RoutedEventHandler已检查; 

我一直在刮目相看。我想到,在我的静态帮助器中 - 声明一个委托(在命名空间级别声明):

  public delegate void UIControlHandler发件人,RoutedEventArgs e); 

然后我的帮助程序如下所示:

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

出现编译错误:


运算符'+ ='不能应用于
'System.EventHandler'和UIControlHandler的操作数

无法将类型UIControlHandler'隐式转换为'System.EventHandler'


任何人都可以帮助我指出正确的方向吗?这是令我疯狂的。

解决方案

关键字:MulticastDelegate



是关于C#中的事件/代表的总体概述。
http://www.codeproject.com/KB/cs/delegates_overview。 aspx



当然你也可以使用事件解除接口。



编辑2: / h1>

我发现这个:如何将事件传递给方法?
这应该有帮助,我不认为你会得到一个更好的解决方案,因为不可能将引用参数传递给匿名方法。


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.

解决方案

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.

这篇关于将事件和委托事件处理程序传递给通用的助手方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆