1

I am in the process of learning ReactiveUI and I am starting with commands

I have trouble translating the code for this RelayCommand to the equivalent ReactiveCommand

GodkendeBilagCommand = new RelayCommand<AdminUdbetalingsKvartal>(OnGodkendeBilag, GodkendeBilagCanExeute);

this is the code for GodkendeBilagCanExeute:

private bool GodkendeBilagCanExeute(AdminUdbetalingsKvartal kvartal)
{
    return kvartal != null && kvartal.KanGodkendeBilag && !IsBusy;
}
Martin Andersen
  • 2,460
  • 2
  • 38
  • 66

1 Answers1

0

How about this:

var canExecute = this.WhenAny(x => x.kvartal.KanGodkendeBilag, x => x.IsBusy,
    (bilag, busy) => bilag.Value && !busy.Value);

GodkendeBilagCommand = ReactiveCommand.Create(canExecute);
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • 1
    Looks nice but the kvartal is a command parameter and not a property on the viewmodel. I have taking a look at Rx test code and I can't find a test where the command parameter is used. – Martin Andersen Dec 21 '14 at 11:03
  • The command parameter is intentionally not used because it is an Antipattern in ReactiveUI – Ana Betts Dec 21 '14 at 13:18
  • 1
    Can you explain to me why it's an AntiPattern? I have INotifyPropertyChanged on many of my domain objects, shame on me (-: and it reduces the amount of code I have to write for simple CRUD forms. If I don't have command parameter how can I then edit a list of customers without putting a command on each customer domain object? – Martin Andersen Dec 29 '14 at 10:11
  • Is there ANY way to have a command parameter passed to the `CanExecute`? Say I have a calculator or another board with many buttons, might even be dynamic, and I want a single command to be bound to them all, having the parameter be statically in the button and pass on as parameter, does that mean I need separate command for each button? Any other ideas? – Shimmy Weitzhandler Nov 17 '19 at 03:58