2017-10-17 81 views
0

我想了解WPF如何实现路由命令,这在内部是由路由事件实现的。在UIElement类的定义,我们发现这段代码:WPF RoutedCommand/RoutedEvent谁/什么引发了这个事件?

EventManager.RegisterClassHandler(type, CommandDevice.CommandDeviceEvent, new 
    CommandDeviceEventHandler(UIElement.OnCommandDeviceThunk), false); 

从那里,该UIElement.OnCommandDeviceThunk处理程序将通过功能在其身上调用其本身做同样的,直到它到达参数沿着所有的信息传递我们订阅的CommandBinding.Executed事件,从而执行我们的响应代码。

我似乎无法找到并希望了解的是谁提出CommandDevice.CommandDeviceEvent

例如,当我将ButtonBase子类对象的Command属性链接到ApplicationCommands命令时,它是否以某种方式引发CommandDeviceEvent,它是如何实现的?

我明白GlobalEventManager拥有事件处理程序列表的分数,但我还没有理解谁引发事件来触发这些处理程序?

回答

2

时,例如,我已经连接的ButtonBase子类对象的Command属性的ApplicationCommands命令,它提高以某种方式CommandDeviceEvent,它是如何做到的呢?

ButtonBase的类调用内部CommandHelpers类的ExecuteCommandSource方法点击时:https://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Controls/Primitives/ButtonBase.cs,c0949dd81fca07f3,references

该方法然后执行实际的命令,即,目的是该ICommand属性返回:https://referencesource.microsoft.com/#PresentationFramework/Framework/MS/Internal/Commands/CommandHelpers.cs,518e4547841e704d