有没有添加CommandBindings来控制VS使用RegisterClassCommandBinding有区别吗? [英] Is there a difference between adding CommandBindings to a control vs using RegisterClassCommandBinding?

查看:358
本文介绍了有没有添加CommandBindings来控制VS使用RegisterClassCommandBinding有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前我一直在使用

this.CommandBindings.Add(
    new CommandBinding(ApplicationCommands.Copy, this.cmdCopy_Executed, this.cmdCopy_CanExecute))

其中cmdCopy_Executed是一个非静态函数,但我采用看到乡亲

where cmdCopy_Executed is a non-static function, but I've seen folks using

static MyControl()
    {
        CommandBinding binding =
            new CommandBinding(ApplicationCommands.Save, CommandHandler);
        CommandManager.RegisterClassCommandBinding(typeof(MyControl), binding);
    }
 private static void CommandHandler(object target, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("Command Handled!");
    }



其中的CommandBinding是静态的。

where the CommandBinding is static. Is one preferred over another?

推荐答案

后者更是一个全球性的处理程序,对前者是每个实例。

The latter is more of a global handler, versus the former which is per instance.

此外,RegisterClassCommandBinding不能被注销,所以你坚持了下来,一旦你注册。一般情况下,使用这种时候最好叫上你的控制虚拟方法,使他们的行为可以被改变或旁路。

Also, the RegisterClassCommandBinding cannot be unregistered, so you are stuck with it once you register. Generally, when using this it's best to call virtual methods on your control so their behavior can be changed or by-passed.

通过CommandBindings您可以删除没有任何绑定不再需要了。这也可以通过你的控制的外部用户完成。所以,你可以添加一个命令绑定是必需的,但有人可以轻易做到 element.CommandBindings.Clear()

With CommandBindings you can remove any bindings that are no longer needed. This can also be done by external users of your control. So you may add a command binding that is required, but someone could easily do element.CommandBindings.Clear().

因此,有差异,每个人都有自己的位置。如果你希望它是很容易定制,我会去同前。

So there are differences, and each has their place. If you want it to be easily customizable, I'd go with the former.

这篇关于有没有添加CommandBindings来控制VS使用RegisterClassCommandBinding有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆