如何使用Debugger(VS Professional 2012)查找事件列表? [英] How to find list of events using Debugger (VS Professional 2012)?
问题描述
好的,我找不到任何帮助,我的问题stackoverflow似乎没有什么,或者我不知道如何寻找它(请纠正我,如果我错了,我会关闭这个问题)。
在我的程序中,我有一个 Grid
,其中有几个事件被隐藏在代码中:
public Grid _grid = new Grid();
_grid.MouseLeftButtonDown + = new MouseButtonEventHandler(MyMethod);
//还有一些事件...
现在在我的程序运行中,我看到一些奇怪的行为,只能来自一些事件,所以我设置一个断点,并停止程序使用调试器。
有没有一个列表,我可以找到某个地方列出成员的所有当前定义的事件 _grid
所以我可以检查没有不需要的事件还没有被删除?
更新2
不幸的是,WPF中的大多数事件(即 UIElement
)通过手动实现 add
/ remove
来实现,这意味着事件成员可以只能在 - =
或 + =
操作符的左侧(即不能读取 )。内部是这样的,每个事件被委派到一个事件的集合,该集合只包含分配事件的元素(例如,如果有一个单独的 MouseLeftButtonDownEvent + = somehandler;
然后事件的集合只有一个条目,不幸的是,事件的集合存储来表示处理程序是一个内部结构,您必须能够实例化来查询集合。您无法实例化该结构的实例( RoutedEventHandlerInfo
,FWIW),以便查询收藏( UIElement.EventHandlersStore._entries
,也是FWIW),例如if您可以,您可以在QuickWatch窗口中查询特定事件的处理程序:
grid.EventHandlersStore._entries [
new RoutedEventHandlerInfo(UIElement.MouseLeftButtonDownEvent,false)]
但是,调试器不允许你调用一个内部的构造函数。
事件没有列出 的东西。您可以在调试器(watch,quickwatch等)中查看实例的所有成员,并且事件具有不同的图标。然后,您可以展开其中的每一个,以查看分配给该事件的方法。例如:
如您所见, MyEvent
已被分配方法 t_MyEvent
更新:
如果您有多个事件处理程序分配给一个事件,那么调试器只会在快速监视中显示事件顶层的最后分配的方法。要查看所有分配的方法,您需要向下钻取到调用列表。例如:
..这表明 t_MyEvent
和 t_MyEvent2
都在 MyEvent
的调用列表。如果您没有处理程序,则 MyEvent
的值将为 null
。
Okay, I can't find any help for my question and stackoverflow doesn't seem to have anything either, or I didn't know how to look for it (please correct me, if I'm wrong and I will close this question).
In my program, I have a Grid
which has a few events definded in code:
public Grid _grid = new Grid();
_grid.MouseLeftButtonDown += new MouseButtonEventHandler(MyMethod);
//and a few more events...
Now during my program run, I saw some weird behaviour that can only come from some events, so I set a breakpoint and stopped the program to use the debugger.
Is there a list I can find somewhere that lists all the currently defined events of the member _grid
so I can check that no unwanted events have not yet been removed?
Update 2
Unfortunately, most events in WPF (i.e. on UIElement
) are implemented by manually implementing add
/remove
which means the event member can only be on the left hand side of a -=
or +=
operator (i.e. it can't be "read"). The internals are such that each event is "delegated" to a collection of events and that collection only contains elements for assigned events (e.g. if there's a single MouseLeftButtonDownEvent += somehandler;
then that collection of events will have only one entry. Unfortunately, what the collection of events stores to represent a handler is an internal structure that you would have to be able to instantiate to query the collection. You are unable to instantiate an instance of that structure (RoutedEventHandlerInfo
, FWIW) in order to query the collection (UIElement.EventHandlersStore._entries
, also FWIW). e.g. if you could you, you could query the handler for a particular event as such in the QuickWatch window:
grid.EventHandlersStore._entries[
new RoutedEventHandlerInfo(UIElement.MouseLeftButtonDownEvent, false)]
But, the debugger does not allow you to invoke an internal constructor.
There isn't something that lists just the events. You can see all the members of a instance in the debugger (watch, quickwatch, etc.) and the events have a distinct icon. You can then expand each one of these to see what method was assigned to the event. For example:
As you can see, MyEvent
has been "assigned" the method t_MyEvent
for this particular instance.
Update: If you have more than one event handler assigned to an event, the debugger will only show the last assigned method in top-level of the event in quick watch. To see all the methods assigned, you'll need to drill-down to the invocation list. For example:
.. this shows that both t_MyEvent
and t_MyEvent2
are in the invocation list for MyEvent
. If you hace no handlers, the value for MyEvent
will be null
.
这篇关于如何使用Debugger(VS Professional 2012)查找事件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!