问题描述
有人可以解释一下WPF中的类级别的事件处理程序吗?我在WPF中使用路由事件,但是目前我读了一本书,我发现作者提到了类级别的事件处理程序。将类处理程序视为路由事件的静态事件处理程序。你可能想要注册这样一个处理程序,如果你想要的,例如,处理所有的鼠标事件没有任何特定的对象实例涉及。你通常会在一个类的静态构造函数中注册:
static MyWindow()
{
EventManager.RegisterClassHandler(typeof(MyWindow),PreviewMouseLeftButtonDownEvent,new RoutedEventHandler(OnMouseLeftButtonDown));
}
另见:
HEllo,can someone explain me what class-level event handler is in WPF? I use routed events in WPF but currently I read a book and I found the author mentions about class-level event handler. What is the practical use of this technique?
Think of class handlers as static event handlers for a routed event. You might want to register such a handler if you want, for example, handle all mouse down events without any particular instance of the object involved. You would typically register it in a static constructor of a class:
static MyWindow()
{
EventManager.RegisterClassHandler(typeof(MyWindow), PreviewMouseLeftButtonDownEvent, new RoutedEventHandler(OnMouseLeftButtonDown));
}
See also:
http://msdn.microsoft.com/en-us/library/ms597875.aspx
http://karlshifflett.wordpress.com/2008/04/22/wpf-sample-series-eventmanagerregisterclasshandler/
这篇关于WPF中的类级事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!