前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF中ICommand接口 的一个设计问题

WPF中ICommand接口 的一个设计问题

作者头像
用户1172223
发布2018-01-29 13:10:36
9740
发布2018-01-29 13:10:36
举报
文章被收录于专栏:哲学驱动设计哲学驱动设计
代码语言:js
复制
    public interface ICommand
    {
        // Summary:
        //     Occurs when changes occur that affect whether or not the command should execute.
        event EventHandler CanExecuteChanged;
        bool CanExecute(object parameter);
        void Execute(object parameter);
    }
    //     Defines an object that knows how to invoke a command.
    public interface ICommandSource
    {
        // Summary:
        //     Gets the command that will be executed when the command source is invoked.
        ICommand Command { get; }
        object CommandParameter { get; }
        IInputElement CommandTarget { get; }
    }

MSDN:

Normally, a command source will listen to the CanExecuteChanged event on the command. This informs the command source when conditions change on the command target, such as loss of keyboard focus. The command source can then query the command using the CanExecute method.

    按照MSDN中的解释,当CanExecuteChanged事件发生时,ICommandSource会调用ICommand的CanExecute方法来检测是否可以执行命令。如果是这样,这里的这个事件名取为:类似“CanExecuteConditionChanged”不是更好吗?

    因为CanExecuteChanged发生,使用者的第一感觉就是CanExecute从false变到true或者由true变到false了。

    关于这个设计问题,我想不出原因,所以希望高人解释下。:)

    另外,还有一个小问题,MSDN中说到:

In the Windows Presentation Foundation (WPF) commanding system, the CommandTarget property on a ICommandSource is only applicable when the ICommand is a RoutedCommand. If the CommandTarget is set on a ICommandSource and the corresponding command is not a RoutedCommand, the command target is ignored.

    那么,设计这个属性会不会有点多余?虽然我觉得加上也是好的。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-01-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档