简体   繁体   English

RegisterClassCommandBinding不触发CanExecute

[英]RegisterClassCommandBinding not firing CanExecute

In certain situations (I couldn't establish a pattern) the execution goes over 在某些情况下(我无法建立模式),执行结束

    System.Windows.Input.CommandManager.InvalidateRequerySuggested();
    var can = GlobalCommands.UpdateWindowTitle.CanExecute(title, null);
    GlobalCommands.UpdateWindowTitle.Execute(title, null);

without entering CanExecute nor Execute methods . 无需输入CanExecute或Execute方法 Variable "can" is false (without executing CanExecute), and the command never fires. 变量“ can”为false(不执行CanExecute),并且该命令从不触发。 But sometimes it works as expected. 但有时它会按预期工作。

This is how the command is created and bound: 这是命令的创建和绑定方式:

    var updateTitleBinding = new CommandBinding(GlobalCommands.UpdateWindowTitle,         UpdateWindowTitle, CanUpdateWindowTitle);
    CommandManager.RegisterClassCommandBinding(typeof(System.Windows.Window), updateTitleBinding);

I can't use 我不能用

    Application.Current.MainWindow.CommandBindings.Add(
         new CommandBinding(_addCommand, ExecuteAddCommand, CanExecuteAddCommand));

because the calling class is not a visual element and doesn't have CommandBindings 因为调用类不是可视元素,并且没有CommandBindings

I tried calling 我尝试打电话

    System.Windows.Input.CommandManager.InvalidateRequerySuggested();

before CanExecute and Execute calls, but this doesn't affect the behaviour. 可以在CanExecute和Execute调用之前进行,但这不会影响行为。

-- -

Do you know what might be happening there? 您知道那里可能会发生什么吗? Are there any other patterns that I could use? 还有其他可以使用的模式吗?

The problem looks similar to CanExecute method going to false problem but I'm not setting focus anywhere. 该问题看起来类似于CanExecute方法,将导致错误问题,但我没有在任何地方设置焦点。

Thanks! 谢谢!

好吧,因为您为目标传递了null,这意味着框架将基于Keyboard.FocusedElement进行路由,因此要么为null(如果应用程序处于非活动状态或键盘焦点位于HwndHost之类的其他位置,则可能发生这种情况或MessageBox)或Keyboard.FocusedElement为非null,但不在Window的视觉/逻辑树内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM