Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

DelegateCommand

Mark Smith edited this page Aug 29, 2016 · 1 revision

DelegateCommand

The DelegateCommand class is an implementation of IDelegateCommand and provides an ICommand implementation based on delegates.

It comes in two variations: DelegateCommand which uses an object as the parameter type, and DelegateCommand<T> which casts the parameter to a T type.

This is a standard ICommand implementation that utilizes delegates similar to the built-in Xamarin.Forms Command class. However, this is not dependent on Xamarin.Forms so the ViewModel can avoid taking a dependency on the framework in the source code.

Tip: you should expose this from a ViewModel as an IDelegateCommand so it can be replaced for testing.

Methods

  • RaiseCanExecuteChanged : raises the CanExecuteChanged event.

Example

public class MyViewModel : SimpleViewModel
{
   public IDelegateCommand DoSomething { get; private set; }

   public MyViewModel()
   {
      DoSomething = new DelegateCommand<string>(OnDoSomething, s => CanDoSomething == true);
   }

   private void DoSomething(string parameter)
   {
     ...
   }
}