传递事件和委托事件处理程序到一个通用的helper方法 [英] Passing an Event and a delegate event handler into a generic Helper Method

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

问题描述

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

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

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



所以,上面的代码,在UI线程分配 buttonControl.Click 事件给事件处理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));



成一个单一的静态的,而是普通的助手方法 - 可以指定任何UI控件事件和事件处理程序。然后该方法将钩两个一起使用UIThreadExecutor类

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.

当然,buttonControl也是任何UI控制 - 与相同类型的不同的事件。例如 - 它可能是一个单选按钮与经过事件

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.

如果我转到了RadioButton.Checked或Button.Click的VS 2010的定义,它们都是一样的类型:

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);
}

这与编译错误出现:

运算符'+ ='不能应用于类型为
'System.EventHandler'和UIControlHandler结果
不能类型UIControlHandler'隐式转换到操作数System.EventHandler

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.

推荐答案

关键词:MulticastDelegate

Keyword: MulticastDelegate

下面大约是在C#中的事件/委托的概述。
http://www.codeproject.com/KB/cs/delegates_overview.aspx 。

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

当然你也可以使用带有事件declerations太接口

Of course you can use interfaces with event declerations too.

我发现这个:如何通过事件的方法?
这应该可以帮助,我不认为你会得到一个更好的解决方案,因为它是不可能的裁判PARAMS传递给anonymus方法。

I found this: How to pass event to 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.

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

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