4

I'm new in WPF trying to use ReactiveUI, I have used ICommand / DelegateCommand one time before, but now I want to use ReactiveCommand

What I'm trying to do is really simple. Click a button in the view, and have that execute a method apply in the view model. I have implemented as below but I'm getting error "cannot convert lambda expression to type system.Iobserver because it is not a delegate type"

private ReactiveCommand _applyCommand;
public ICommand ApplyCommand
        {
            get { return _applyCommand; }
        }

      void   Bind()
{
             _applyCommand = new ReactiveCommand();
            _applyCommand.Subscribe(_ =>
            {
               Apply();
            });
}
 void Apply()
{
}
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
user3106005
  • 179
  • 3
  • 20

1 Answers1

2

I've always found ReactiveCommmands a lot easier to use if you use the static Create(..) method instead of just constructing them.

// This works just like Josh Smith’s RelayCommand
var cmd = ReactiveCommand.Create(x => true, x => Console.WriteLine(x));

The first argument is when the Command should be enabled, always in this case, but more commonly you pass in an observable that emits true or false. The second lambda is the actual operation to call. You don't have to use this, but its always a good first start until you get used to the syntax.

There's a load more help on http://ReactiveUI.net but I'd recommend reading through the guide http://reactiveui.net/welcome/pdf

AlSki
  • 6,868
  • 1
  • 26
  • 39
  • Thanks, i have another problem I want to perform some operation based on property change – user3106005 May 29 '14 at 10:04
  • Thanks, i have another problem I want to call 3 different method based on property value changed. so public int Number { set { _number = val; if (_number == 1) Call1() else if (_number == 2) call2() else if (_number == 3) call3() } } above is working but now I'm trying using ReactiveUI so what i did this.ObservableForProperty(x => x._number ) .Subscribe(_ => Call1()); but how can i add switch statement in above ? – user3106005 May 29 '14 at 10:12