Silverlight控件-是否已定义事件处理程序? [英] Silverlight Control - Is there an event handler already defined?
问题描述
嗨
有没有办法找出控件是否已经定义了任何(或多少)个事件处理程序?
我正在使用Silverlight 4和C#.
我想创建一个事件处理程序,但前提是该控件尚未定义任何事件处理程序.
只算他们就可以了.
Hi
Is there a way to find out if a control has any (or how many) event handler already defined?
I''m using Silverlight 4 and C#.
I want to create an event handler but only if the control has no event handlers already defined.
Just counting them would do it.
LayoutRoot.MouseEnter += new MouseEventHandler(Volta_a_abrir);
只需要知道在LayoutRoot
控件上定义了多少个事件处理程序.
能做到吗?
谢谢
Just need to know how many event handlers are defined on the LayoutRoot
control.
Can it be done?
Thanks
推荐答案
只需在控件上按F12键(在代码中),您将看到基础类(定义).
由此,您可以算出已经定义的事件数.
Yuu可以查看类的层次结构,在Silverlight中,您会注意到大多数类都是从FrameworkElement
派生的.结果,您还将在其他类中看到FrameworkElement
中描述的所有事件.
http://msdn.microsoft.com/en-us/library/system.windows. frameworkelement.aspx [ ^ ]-此链接描述了此控件内的类层次结构和事件,以及派生类中可用的事件.
同样,您可以查看ContentControl
,ItemsControl
etc,它们的事件及其派生类.
此外,类似ILDASM的工具可以帮助您查看程序集后面的IL.这也可能有助于您了解各种程序集和类中正在使用的事件.
Just press F12 on the control (in code) and you will see the underlying class (definition).
From this, you can figure out the number of events that are already defined.
Yuu can view the class hierarchy structure, and in Silverlight you will notice most classes are derived fromFrameworkElement
. As a result, you will see all of the events described inFrameworkElement
in other classes as well.
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.aspx[^] - this link describes the class hierarchy and events inside this control and the events that would be available in derives classes.
Similarly, you can look atContentControl
,ItemsControl
etc, their events and their derived classes.
Further, a tool like ILDASM can help you look at the IL behind assemblies. This might help you get an idea on events that are being used in various assemblies and classes as well.
我想您正在寻找这样的东西:
I guess you are looking for something like this:
public event EventHandler onevent;
private void func()
{
EventHandler handlers = onevent;
Delegate[] invocaltionList = handlers.GetInvocationList();
if (invocaltionList.Length == 0)
{
onevent += OnEventHandler;
}
}
public void OnEventHandler(object sender, EventArgs ea)
{
}
GetInvocationList返回委托的调用列表,其中数组的每个元素恰好表示一个方法.
最好的问候
Espen Harlinn
GetInvocationList returns the invocation list of the delegate where each element of array represents exactly one method.
Best regards
Espen Harlinn
这篇关于Silverlight控件-是否已定义事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!