简体   繁体   English

Relay / ICommand vs DelegateCommand - 差异

[英]Relay/ICommand vs DelegateCommand — Differences

As far as I can tell the below code can be changed from Relay/ICommand Command to Delegate command and still bind the commands the same way! 据我所知,下面的代码可以从Relay / ICommand Command更改为Delegate命令,并且仍然以相同的方式绑定命令! If I am wrong what are the differences and uses of each. 如果我错了,每个人的差异和用途是什么。

private DelegateCommand something;
public DelegateCommand Something

Here is the full implementation 这是完整的实现

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);
        }
    }
}

Neither DelegateCommand nor RelayCommand exist in the framework itself. DelegateCommandRelayCommand都不存在于框架本身中。 They are provided by third party libraries. 它们由第三方图书馆提供。

Both are an implementation of ICommand which works by accepting a delegate and using that to provide the ICommand implementation. 两者都是ICommand的实现,它通过接受委托并使用它来提供ICommand实现来工作。 As such, both classes have the same intent, and work in basically the same manner. 因此,两个类具有相同的意图,并且以基本相同的方式工作。

As for differences - there may be some subtle diffferences, depending on which framework you're using. 至于差异 - 可能存在一些微妙的差异,具体取决于您使用的框架。 For example, Prism's DelegateCommand<T> also has a concept of IActiveAware , which is used for building composite commands. 例如,Prism的DelegateCommand<T>也有一个IActiveAware概念,用于构建复合命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM