JFoulkes
JFoulkes

Reputation: 2429

Bind to IReactiveCommand in view code behind

I have my ViewModel which has an ErrorCommand. I wish to subscribe to this in my view code behind so that any time it is called I can display an error message which is passed like so:

ErrorCommand.Exectute("Error occured")

In the view:

this.WhenAny(view => x.ViewModel.ErrorCommand, x => x.Value).Subscribe(error => DisplayError(error));

This code doesn't actually work but hopefully shows what I'm trying to acheive. How would I do this correctly?

I understand I could use the MessageBus, but I also have a similar scenario where the MessageBus wouldn't be appropriate.

Upvotes: 0

Views: 196

Answers (2)

Ana Betts
Ana Betts

Reputation: 74654

There's a method specifically for this scenario:

this.WhenAnyObservable(x => x.ViewModel.ErrorCommand).Subscribe(x => /* ... */);

will do what you expect and will avoid the null refs

Upvotes: 2

Oliver Weichhold
Oliver Weichhold

Reputation: 10296

this.WhenAny(view => x.ViewModel.ErrorCommand, x => x.Value).Subscribe(error => DisplayError(error));

Will only fire when you change the value of the ErrorCommand property.

What you want is this:

ViewModel.ErrorCommand.IsExecuting.Subscribe(x=> DisplayError(x));

Upvotes: 0

Related Questions