为什么要使用RelayCommand或DelegateCommand而不是仅实现ICommand? [英] Why use a RelayCommand or DelegateCommand instead of just implementing ICommand?

查看:338
本文介绍了为什么要使用RelayCommand或DelegateCommand而不是仅实现ICommand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习WPF中的MVVM,对于MVVM,我对WPF来说都是全新的(我了解它是如何工作的,但从未使用过...)

I'm just learning about MVVM in WPF, I'm completely new both to WPF as to MVVM (I understand how it works, but have never used it...)

我在网上找到的每个教程/文章都使用RelayCommand或DelegateCommand.

Every single tutorial/article I find on the web, it uses either RelayCommand, or the DelegateCommand.

在我看来,这些模式使VM违反了SRP原则,因为它会将命令逻辑保存在其中.

In my opnion, these patterns obligues the VM to violate the SRP principle, since it will hold the command logic inside them.

为什么不只使用ICommand接口的自定义实现?就是这样:

Why not just use a custom implementation of the ICommand interface? Just like this:

想象一下,您正在显示一个人并将其保存到数据库中:

Imagine that you're displaying a person and saving it to the DB:

我的Xaml是这样的:

My Xaml would be this:

<StackPanel>                         
    <TextBlock Width="248" Height="24" Text="The name is:: " />
    <TextBlock Width="248" Height="24" Text="{Binding Name}">            
    </TextBlock>
    <TextBox HorizontalAlignment="Left" Name="textBox1" Width="120" Height="23" 
             VerticalAlignment="Top" Text="{Binding Name}"
             />
    <Button  Name="Salvar" VerticalAlignment="Bottom" 
            Command="{Binding SavePerson}" 
            CommandParameter="{Binding}">Save</Button>
</StackPanel>

这是我的虚拟机:

public class PersonVM: INotifyPropertyChanged
{
    private string nameValue;

    public string Name
    {
        get{
            return nameValue;
        }
        set
        {
            if (value != this.nameValue)
            {
                this.nameValue= value;
                NotifyPropertyChanged("Name");
            }
        }
    }


    public ICommand SavePerson{ get { return new SavePersonCommand(); } }
    #region INotifyPropertyChanged Members


    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    #endregion
}

这是我的命令:

public class SavePersonCommand: ICommand
{       
    #region ICommand Members

    public bool CanExecute(object parameter)
    {
        return (parameter as PersonVM) != null;            
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
            PersonVM person = parameter as PersonVM;
            if(person != null)
                //Actually Save the person...            
    }   


    #endregion
}

我的方法有什么问题?

推荐答案

如果您不使用某些基本命令(框架或您自己的自定义命令),您会发现自己一遍又一遍地编写相同的代码.例如:您不会在自己的命令中引发CanExecuteChanged事件.实现INotifyPropertyChanged也是一样.这就是每个人都使用一个或另一个MVVM框架的原因.

If you won't use some base command (of a framework or your own custom command) you'll find yourself writing the same code over and over again. For example: you don't raise CanExecuteChanged event in your own command. The same goes for implementing INotifyPropertyChanged. That's why everyone's using one or another MVVM framework.

这篇关于为什么要使用RelayCommand或DelegateCommand而不是仅实现ICommand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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