Relay/ICommand vs DelegateCommand -- Differences

本文关键字:Differences DelegateCommand vs ICommand Relay | 更新日期: 2023-09-27 18:17:20

据我所知,下面的代码可以从Relay/ICommand命令更改为Delegate命令,并且仍然以相同的方式绑定命令!如果我错了,它们的区别和用途是什么?

private DelegateCommand something;
public DelegateCommand Something

下面是完整的实现

private RelayCommand something;
public ICommand Something
{
    get
    {
        if (something == null)
            something = new RelayCommand(SomethingMethod, CanSomething);
        return something;
    }
}
private bool CanSomething(object parameter)
{
    //just for readability return true
    return true;
}
private void SomethingMethod(object parameter)
{
    using (DatabaseContext context = new DatabaseContext())      
    {
        try { }
        catch(Exception ex)
        {
            throw new ApplicationException(string.Format("Something {0} to {1}", file, directory), ex);
        }
    }
}

Relay/ICommand vs DelegateCommand -- Differences

框架本身不存在DelegateCommandRelayCommand。它们由第三方库提供。

都是ICommand的实现,通过接受委托并使用它来提供ICommand实现。因此,这两个类具有相同的意图,并且以基本相同的方式工作。

至于差异—可能会有一些细微的差异,这取决于您使用的是哪个框架。例如,Prism的DelegateCommand<T>也有IActiveAware的概念,用于构建复合命令。