user3106005
user3106005

Reputation: 189

Using ReactiveCommand

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()
{
}

Upvotes: 4

Views: 2143

Answers (1)

AlSki
AlSki

Reputation: 6961

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

Upvotes: 2

Related Questions