为什么RelayCommand [英] Why RelayCommand

查看:148
本文介绍了为什么RelayCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在最近WPF编程了很多,但我的查看和视图模型并不在这一点上分开。那么,它的部分。有关在文本框中的文本,所有我的绑定,对标签内容,在DataGrid中列出,......通过定期性质与他们NotifyPropertyChanged事件完成。

I've been programming a lot in WPF lately but my View and ViewModel are not separate at this point. Well, it's partially. All my bindings concerned to text in text boxes, content for labels, lists in datagrids, ... are done by regular properties with a NotifyPropertyChanged event in them.

我所有的处理按钮点击或文本改变的东西的事件是由联的事件完成。现在,我想使用命令启动工作,并发现这篇文章:<一href=\"http://www.$c$cproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute\">http://www.$c$cproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute.它有如何建立一个MVVM解释,但我很困惑与 RelayCommand

All my events for handling button clicks or text changed stuff is done by linking the events. Now, I wanted to start working with commands and found this article: http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute. It has an explanation of how to set up MVVM but I'm confused with the RelayCommand.

它做什么工作?
它是可用在我的所有形式的命令?
怎样使按钮关闭时(a)某些文本框(ES),不会填充?

What job does it do? Is it useable for all commands in my form? How do I make the button disable when (a) certain text box(es) are not filled in?

编辑1:

一个很好的解释为它是可用在我的表单中的所有的命令?在这里回答:<一href=\"http://stackoverflow.com/a/22286816/3357699\">http://stackoverflow.com/a/22286816/3357699

A good explanation to "Is it useable for all commands in my form?" is answered here: http://stackoverflow.com/a/22286816/3357699

下面是code我到目前为止有:<一href=\"http://stackoverflow.com/a/22289358/3357699\">http://stackoverflow.com/a/22289358/3357699

Here is the code I have so far: http://stackoverflow.com/a/22289358/3357699

推荐答案

命令用于分离的语义和从执行命令即它分离UI组件逻辑调用命令对象从需要在命令调用执行的逻辑。所以,你可以测试单独的业务逻辑使用测试案件,并您的UI code是松散耦合的业务逻辑。

Commands are used to separate the semantics and the object that invokes a command from the logic that executes the command i.e. it separates UI component from the logic that needs to be executed on command invocation. So, that you can test business logic separately using test cases and also your UI code is loosely coupled to business logic.

现在,话虽这么说,让我们挑一个你的问题之一:

Now, that being said let's pick your questions one by one:

它做什么工作?

我已经加入上述的细节。希望它清除命令的用法。

I have added the details above. Hope it clears the usage of commands.

它是可用于在我的表单中的所有命令?

Is it usable for all commands in my form?

有些控件暴露命令的DependencyProperty像按钮,菜单项等,这些都注册到它的一些默认事件。对于按钮是点击事件。所以,如果你绑定的ICommand 宣布与视图模型巴顿的指挥DP,它会被调用,只要按钮被点击。

Some controls exposed Command DependencyProperty like Button, MenuItem etc. which have some default event registered to it. For Button it's Click event. So, if you bind ICommand declared in ViewModel with Button's Command DP, it will be invoked whenever button is clicked on.

有关的其他事件,您可以使用绑定交互触发。参考样品此处如何使用它们绑定到的ICommand 在视图模型。

For other events, you can bind using Interactivity triggers. Refer to the sample here how to use them to bind to ICommand in ViewModel.

如何使按钮关闭时(a)某些文本框(ES)不
  填写?

How do I make the button disable when (a) certain text box(es) are not filled in?

您发布不提供 RelayCommand 的完整实现的链接。它缺乏重载的构造函数来设置 CanExecute predicate其中起着启用/禁用到您的命令绑定到UI控件了关键作用。

The link you posted doesn't provide complete implementation of RelayCommand. It lacks the overloaded constructor to set CanExecute predicate which plays a key role in enabling/disabling the UI control to which your command is bind to.

在视图模型,并在 CanExecute 委托一些属性绑定文本框,如果任何绑定属性为null或空时自动禁用该命令被绑定到控制返回false

Bind TextBoxes with some properties in ViewModel and in CanExecute delegate returns false if any of the binded property is null or empty which automatically disabled the control to which command is binded to.

全面实施 RelayCommand

public class RelayCommand<T> : ICommand
{
    #region Fields

    readonly Action<T> _execute = null;
    readonly Predicate<T> _canExecute = null;

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
    /// </summary>
    /// <param name="execute">Delegate to execute when Execute is called on the command.  This can be null to just hook up a CanExecute delegate.</param>
    /// <remarks><seealso cref="CanExecute"/> will always return true.</remarks>
    public RelayCommand(Action<T> execute)
        : this(execute, null)
    {
    }

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

        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion

    #region ICommand Members

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

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

    ///<summary>
    ///Defines the method to be called when the command is invoked.
    ///</summary>
    ///<param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to <see langword="null" />.</param>
    public void Execute(object parameter)
    {
        _execute((T)parameter);
    }

    #endregion
}

这篇关于为什么RelayCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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