Reputation: 5734
I'm trying to create a ReactiveCommand within the constructor of a ViewModel:
IObservable<bool> canExecute = this
.WhenAnyValue(x => x.InsertableItems, x => x.IsRefreshing,
(insertableItems, isRefreshing) => !isRefreshing && insertableItems > 0);
canExecute.Subscribe(x => this.Logger.LogInformation("Can execute changed: {0}", x));
this.ConvertCmd = ReactiveCommand.CreateAsyncTask(canExecute, ConvertCmdExecute);
ConvertCmd is then bound to the Command property of a button in my GUI,
<Button Content="Convert" Command="{Binding ConvertCmd, Mode=OneWay}" />
However, the GUI button remains greyed out and never becomes enabled
A few things to note:
I'm a loss here. Does anyone have an idea of why the button never becomes enabled in my original scenario?
Upvotes: 0
Views: 1592
Reputation: 5734
As usual, after spending hours on the issue, I find my own answer just moments after posting this...
Seems like this was a threading issue. The Subscriptions got executed on unknown, potentially varying threads. The following fixes the code - even though to be honest, I'm not entirely sure why it is necessary to have this run on the Dispatcher.
IObservable<bool> canExecute = this
.WhenAnyValue(x => x.InsertableItems, x => x.IsRefreshing,
(insertableItems, isRefreshing) => !isRefreshing && insertableItems > 0)
.ObserveOnDispatcher(); // this fixes the issue
Upvotes: 2