简体   繁体   中英

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! 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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