WPF Routed命令有时仅触发

WPF Routed命令有时仅触发,wpf,canvas,focus,routedcommand,Wpf,Canvas,Focus,Routedcommand,我有一些路由命令,比如control-A、copy-paste,它们都可以正常工作。 然后我又添加了4个routedcommands来使用箭头键在画布中上下左右移动对象,它们有时有效,有时无效。起初,我认为这是画布上的一个焦点问题,但我刚刚发现,同时,所有其他路由命令(如control-a)都可以工作,但箭头键不能。 我真的不知道这里发生了什么,它们是相同的RoutedCommand,具有不同的变量名,为什么一个命令在100%的时间内工作,而另一个命令只在50%的时间内工作 工作路由命令: _b

我有一些路由命令,比如control-A、copy-paste,它们都可以正常工作。 然后我又添加了4个routedcommands来使用箭头键在画布中上下左右移动对象,它们有时有效,有时无效。起初,我认为这是画布上的一个焦点问题,但我刚刚发现,同时,所有其他路由命令(如control-a)都可以工作,但箭头键不能。 我真的不知道这里发生了什么,它们是相同的RoutedCommand,具有不同的变量名,为什么一个命令在100%的时间内工作,而另一个命令只在50%的时间内工作

工作路由命令:

_bindings.Add(new CommandBinding(DesignerCanvas.SelectAll, SelectAll_Executed));
SelectAll.InputGestures.Add(new KeyGesture(Key.A, ModifierKeys.Control));

private void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
{
    SelectionService.SelectAll();
}
_bindings.Add(new CommandBinding(DesignerCanvas.MoveDown, MoveDown_Executed));
MoveDown.InputGestures.Add(new KeyGesture(Key.Down));

private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e)
{
    e.Handled = true;
    var selectedItems = from item in SelectionService.CurrentSelection.OfType<DesignerItem>()
                            select item;

    if (selectedItems.Count() > 0)
    {
        for (int i = 0; i < selectedItems.Count(); i++)
            selectedItems.ElementAt(i).Top += Option.OptionSingleton.Sensitivity;
    }
}
RoutedCommand出现故障:

_bindings.Add(new CommandBinding(DesignerCanvas.SelectAll, SelectAll_Executed));
SelectAll.InputGestures.Add(new KeyGesture(Key.A, ModifierKeys.Control));

private void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
{
    SelectionService.SelectAll();
}
_bindings.Add(new CommandBinding(DesignerCanvas.MoveDown, MoveDown_Executed));
MoveDown.InputGestures.Add(new KeyGesture(Key.Down));

private void MoveDown_Executed(object sender, ExecutedRoutedEventArgs e)
{
    e.Handled = true;
    var selectedItems = from item in SelectionService.CurrentSelection.OfType<DesignerItem>()
                            select item;

    if (selectedItems.Count() > 0)
    {
        for (int i = 0; i < selectedItems.Count(); i++)
            selectedItems.ElementAt(i).Top += Option.OptionSingleton.Sensitivity;
    }
}

发生故障的RoutedCommand有时只是不触发,特别是在我打开其他窗口并返回画布后,它将停止触发,而其他RoutedCommand则不受影响。知道是什么导致了这种奇怪的行为吗?

有些人可以使用非常包容的类事件处理程序来跟踪事件的路径:

EventManager.RegisterClassHandler(typeof(FrameworkElement), CommandManager.CanExecuteEvent, 
     new CanExecuteRoutedEventHandler((s, e) => Debug.WriteLine("CanExecute: " + s)), true);
EventManager.RegisterClassHandler(typeof(FrameworkElement), CommandManager.ExecutedEvent, 
     new CanExecuteRoutedEventHandler((s, e) => Debug.WriteLine("Executed:" + s)), true);
EventManager.RegisterClassHandler(typeof(FrameworkElement), CommandManager.ExecutedEvent, 
     new CanExecuteRoutedEventHandler((s, e) => Debug.WriteLine("KeyDown:" + s)), true);
在您的情况下,KeyDown可能在到达命令绑定之前被处理,或者CanExecute事件可能由于其他原因无法到达它


希望这将帮助您调试问题

您可以使用非常包容的类事件处理程序来跟踪事件的路由:

EventManager.RegisterClassHandler(typeof(FrameworkElement), CommandManager.CanExecuteEvent, 
     new CanExecuteRoutedEventHandler((s, e) => Debug.WriteLine("CanExecute: " + s)), true);
EventManager.RegisterClassHandler(typeof(FrameworkElement), CommandManager.ExecutedEvent, 
     new CanExecuteRoutedEventHandler((s, e) => Debug.WriteLine("Executed:" + s)), true);
EventManager.RegisterClassHandler(typeof(FrameworkElement), CommandManager.ExecutedEvent, 
     new CanExecuteRoutedEventHandler((s, e) => Debug.WriteLine("KeyDown:" + s)), true);
在您的情况下,KeyDown可能在到达命令绑定之前被处理,或者CanExecute事件可能由于其他原因无法到达它


希望这将帮助您调试问题

这可能是因为您使用的键是向下键。我怀疑如果你使用不同的钥匙,它会工作

某些控件使用箭头键和pageup/pagedown键。例如,TextBox就是这样做的。如果画布在scrollviewer中,scrollviewer可能正在吃掉它

有两种解决方法:

向正在执行关键点手势的控件添加绑定。 处理画布或正在执行击键的控件的任何父控件的KeyPreview,并从那里执行命令。
的答案显示了如何在不为每个命令在KeyPreview处理程序中写入特定代码的情况下执行2。

这可能是因为您使用的键是向下键。我怀疑如果你使用不同的钥匙,它会工作

某些控件使用箭头键和pageup/pagedown键。例如,TextBox就是这样做的。如果画布在scrollviewer中,scrollviewer可能正在吃掉它

有两种解决方法:

向正在执行关键点手势的控件添加绑定。 处理画布或正在执行击键的控件的任何父控件的KeyPreview,并从那里执行命令。
的答案显示了如何在不为每个命令在KeyPreview处理程序中编写特定代码的情况下执行2。

事实证明这是一个焦点问题,我只是在鼠标进入时将焦点设置到画布上,现在它有点固定了。谢谢大家的回答。

事实证明这是一个焦点问题,我只是在鼠标进入时将焦点设置到画布上,现在它有点固定了。谢谢大家的回答