I would like to know the recommended way to bind to ReactiveCommand IsExecuting .
The problem is that the initial execution of the command (started at the end of the constructor) does not update the WPF control using IsLoading as a binding, although subsequent calls work as expected.
Update 2 Add Code Binding Code
It shows adorner content when IsLoading true
<ac:AdornedControl IsAdornerVisible="{Binding IsLoading}"> <ac:AdornedControl.AdornerContent> <controls1:LoadingAdornerContent/> </ac:AdornedControl.AdornerContent> <fluent:ComboBox ItemsSource="{Binding Content, Mode=OneWay}" DisplayMemberPath="Name" SelectedValuePath="ContentId" SelectedValue="{Binding SelectedContentId}" IsSynchronizedWithCurrentItem="True" /> </ac:AdornedControl>
Update
I found this: https://github.com/reactiveui/rxui-design-guidelines
and realized that I should do something like:
this._isLoading = this.WhenAnyValue(x => x.LoadCommand.IsExecuting) .ToProperty(this, x => x.IsLoading);
but it gives a compilation error:
Type arguments for the method "ReactiveUI.OAPHCreationHelperMixin.ToProperty <TObj, TRet> (System.IObservable <TRet>, TObj, System.Linq.Expressions.Expression <System.Func <TObj, TRet →, TRet, System.Reactive.Concurrency. IScheduler) 'cannot be deduced from Application. Try to explicitly specify type arguments.
I also tried:
this._isLoading = this.WhenAnyValue(x => x.LoadCommand.IsExecuting) .ToProperty<TheViewModel, bool>(this, x => x.IsLoading);
but get a compilation error:
'System.IObservable <System.IObservable <bool →' does not contain the definition of “ToProperty” and the best overload of the extension method is “ReactiveUI.OAPHCreationHelperMixin.ToProperty <TObj, TRet> (System.IObservable <TRet>, TObj, System.Linq.Expressions. Expression <System.Func <TObj, TRet →, TRet, System.Reactive.Concurrency.IScheduler) 'has some invalid arguments
and
Instance argument: cannot convert from 'System.IObservable>' to 'System.IObservable'
Original below
The code at the end of my post works for the initial binding, accessing the IsLoading property, and it looks like this is starting a subscription. But from further reading it seems that I should use WhenAny , and I can’t understand what was put in front of my nose:
ToProperty and BindTo - get the initial value without a subscription
Addendum:
this.WhenAnyValue(x => x.LoadCommand.IsExecuting);
also works, but is there a better way?
I was thinking about removing the ObservableAsPropertyHelper , as it does not seem to do much for me and makes IsLoading normal property like:
private bool _isLoading; public bool IsLoading { get { return _isLoading; } set { this.RaiseAndSetIfChanged(ref _isLoading, value); } }
And we will do something like the following, but it does not compile because it is trying to assign an IObservable< bool> to bool:
this.WhenAnyValue(x => x.LoadCommand.IsExecuting) .Subscribe(x => IsLoading = x);
Current code:
private readonly ObservableAsPropertyHelper<bool> _isLoading; public bool IsLoading { get { return _isLoading.Value; } } LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => {