我应该从调用程序code执行之前检查一个ICommand的CanExecute方法? [英] Should I check an ICommand's CanExecute method before calling Execute from procedural code?

查看:172
本文介绍了我应该从调用程序code执行之前检查一个ICommand的CanExecute方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的ICommand S在XAML,WPF使用 CanExecute 的方法来启用或禁用与命令相关联的控制。但是,如果我打电话执行从程序code?我应该先检查 CanExecute ,以确保该命令可以执行,还是应该执行照顾这个检查我?

在换句话说,我应该这样做:

 如果(someCommand.CanExecute(参数,目标))
    someCommand.Execute(参数,目标);
 

或者只是这样的:

  someCommand.Execute(参数,目标);
 

解决方案

好风格将决定你应该做前,先检查CanExecute。这将强制执行适当的分解和实施的一致性。此外,在事件中,你永远不希望使用此命令绑定到一个按钮,它会正常工作。

When using ICommands in XAML, WPF uses the CanExecute method to enable or disable controls associated with the command. But what if I am calling Execute from procedural code? Should I first check CanExecute to make sure that the command can execute, or should Execute take care of this check for me?

In other words, should I do this:

if (someCommand.CanExecute(parameter, target))
    someCommand.Execute(parameter, target);

Or just this:

someCommand.Execute(parameter, target);

解决方案

Good style would dictate that you should do the former, check CanExecute first. This will enforce proper decomposition and a consistency in implementation. Also, in the event you ever do want to use this command bound to a button, it will work as expected.

这篇关于我应该从调用程序code执行之前检查一个ICommand的CanExecute方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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