Commands¶
Prefer binding user interactions to commands rather than methods.
Do¶
// In the view
this.BindCommand(ViewModel, vm => vm.DeleteCommand, v => v.deleteButton);
public class RepositoryViewModel : ReactiveObject
{
public RepositoryViewModel()
{
DeleteCommand = ReactiveCommand.CreateFromObservable(DeleteImpl);
DeleteCommand.ThrownExceptions.Subscribe(ex => /*...*/);
}
public ReactiveCommand<Unit, Unit> DeleteCommand { get; }
private IObservable<Unit> DeleteImpl() {...}
}
Don't¶
Use the Caliburn.Micro conventions for associating buttons and commands:
// In XAML
<Button x:Name="Delete" .../>
public class RepositoryViewModel : PropertyChangedBase
{
public void Delete() {...}
}
Why?¶
- ReactiveCommand exposes the
CanExecuteproperty of the command to enable applications to introduce additional behavior. - It handles marshaling the result back to the UI thread.
- It tracks in-flight items.