我想解决的根本问题是收集我的应用程序上的所有用户输入。点击,击键等。并获得输入的日志元素。 为了实现这一点,我已经将PreviewKeyDown和PreviewMouseDown处理程序添加到主窗口,它工作正常。但我的应用程序基于一个大框架,这个框架可能会显示一些对话框。我需要一种方法来处理这样的对话框并为它们附加处理程序。有没有办法为此创建一个集中处理程序?
答案 0 :(得分:0)
由于UI-Events通常为RoutedEvents,因此您只需注册一个自己的方法来处理该事件的每次调用。例如,如果您希望了解PreviewKeyDownEvent
如何通过您的用户界面进行隧道传输:
EventManager.RegisterClassHandler(
typeof(UIElement),
UIElement.PreviewKeyDownEvent,
new RoutedEventHandler(localMethod));
使用此功能,您可以“追踪”MouseDown
事件隧道,并在单击按钮时冒泡您的可视树:
static UiEventHandler()
{
EventManager.RegisterClassHandler(
typeof(UIElement), UIElement.PreviewMouseDownEvent,
new RoutedEventHandler(PreviewMouseDown));
EventManager.RegisterClassHandler(
typeof(UIElement), UIElement.MouseDownEvent,
new RoutedEventHandler(MouseDown));
}
private static void MouseDown(object sender, RoutedEventArgs e)
{
Debug.WriteLine("MouseDown on " + sender.GetType());
}
private static void PreviewMouseDown(object sender, RoutedEventArgs e)
{
Debug.WriteLine("PreviewMouseDown on " + sender.GetType());
}
输出:
//Event hits Window and tunnels through the visual tree
PreviewMouseDown on WpfApplication10.MainWindow
PreviewMouseDown on System.Windows.Controls.Border
PreviewMouseDown on System.Windows.Documents.AdornerDecorator
PreviewMouseDown on System.Windows.Controls.ContentPresenter
PreviewMouseDown on System.Windows.Controls.Grid
PreviewMouseDown on System.Windows.Controls.Button
PreviewMouseDown on Microsoft.Windows.Themes.ButtonChrome
PreviewMouseDown on System.Windows.Controls.ContentPresenter
PreviewMouseDown on System.Windows.Controls.TextBlock
//Nobody handled the event, so it bubbles back up
MouseDown on System.Windows.Controls.TextBlock
MouseDown on System.Windows.Controls.ContentPresenter
MouseDown on Microsoft.Windows.Themes.ButtonChrome
//ButtonChrome handled the event as a 'Click'