首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么选择RelayCommand

为什么选择RelayCommand
EN

Stack Overflow用户
提问于 2014-03-10 02:00:16
回答 2查看 84.8K关注 0票数 54

我最近一直在用WPF编程,但我的视图和ViewModel在这一点上并不是分开的。好吧,这只是一部分。我的所有绑定都与文本框中的文本、标签的内容、数据网格中的列表有关...由带有NotifyPropertyChanged事件的常规属性完成。

我所有用于处理按钮点击或文本更改的事件都是通过链接这些事件来完成的。现在,我想开始使用命令,并找到了这篇文章:

http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

..。它解释了如何设置MVVM,但我对

..。

它做什么工作?它是否可用于我表单中的所有命令?当(a)某些文本框未填写时,如何使按钮禁用?

编辑1:

对“它是否可用于我表单中的所有命令?”有一个很好的解释。答案如下:

https://stackoverflow.com/a/22286816/3357699

以下是我到目前为止拥有的代码:

https://stackoverflow.com/a/22289358/3357699

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-10 03:14:20

命令用于

将调用命令的语义和对象与执行命令的逻辑分开

即,它将UI组件与需要在命令调用时执行的逻辑分开。因此,您可以使用测试用例单独测试业务逻辑,并且您的UI代码与业务逻辑是松散耦合的。

现在,也就是说,让我们逐一挑选你的问题:

它做什么工作?

我已经添加了上面的细节。希望它能清除命令的用法。

它是否适用于我表单中的所有命令?

一些控件公开命令DependencyProperty,如Button、MenuItem等,它们注册了一些默认事件。对于Button,它是

事件。因此,如果您绑定

使用按钮的命令DP在ViewModel中声明,它将在按钮被单击时被调用。

对于其他事件,可以使用

..。请参阅示例

这里

如何使用它们绑定到

在ViewModel中。

当(a)某些文本框未填写时,如何使按钮禁用?

您发布的链接没有提供完整的实现

..。它缺少要设置的重载构造函数

谓词,它在启用/禁用命令绑定到的UI控件方面起着关键作用。

将TextBoxes与ViewModel和中的某些属性绑定

如果任何绑定的属性为null或empty,则委托返回false,这将自动禁用命令绑定到的控件。

全面实施

代码语言:javascript
复制
public class RelayCommand : ICommand
{
    #region Fields

    readonly Action _execute = null;
    readonly Predicate _canExecute = null;

    #endregion

    #region Constructors

    /// 
    /// Initializes a new instance of .
    /// 
    /// Delegate to execute when Execute is called on the command.  This can be null to just hook up a CanExecute delegate.
    ///  will always return true.
    public RelayCommand(Action execute)
        : this(execute, null)
    {
    }

    /// 
    /// Creates a new command.
    /// 
    /// The execution logic.
    /// The execution status logic.
    public RelayCommand(Action execute, Predicate canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion

    #region ICommand Members

    ///
    ///Defines the method that determines whether the command can execute in its current state.
    ///
    ///Data used by the command.  If the command does not require data to be passed, this object can be set to null.
    ///
    ///true if this command can be executed; otherwise, false.
    ///
    public bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute((T)parameter);
    }

    ///
    ///Occurs when changes occur that affect whether or not the command should execute.
    ///
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    ///
    ///Defines the method to be called when the command is invoked.
    ///
    ///Data used by the command. If the command does not require data to be passed, this object can be set to .
    public void Execute(object parameter)
    {
        _execute((T)parameter);
    }

    #endregion
}
票数 90
EN

Stack Overflow用户

发布于 2014-03-10 02:53:05

使用中继命令的好处是您可以将命令直接绑定到ViewModels。通过以这种方式使用命令,您可以避免在视图代码背后编写代码。

使用中继命令时,您必须提供两种方法。第一个提供是否可以执行命令的值(例如"CanExecuteSave"),而另一个将负责执行命令("ExecuteSave")。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22285866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档