Reputation: 142
I'm currently working on an AvaloniaUI
app and faced this problem.
I have 3 ReactiveCommand
s:
public ReactiveCommand<Unit, Unit> MyCommand1 {get; set;}
public ReactiveCommand<Unit, Unit> MyCommand2 {get; set;}
public ReactiveCommand<Unit, Unit> MyCommand3 {get; set;}
which are initialized in constructor like that:
public MyClass()
{
MyCommand1 = ReactiveCommand.CreateFromTask(MyMethod1);
MyCommand2 = ReactiveCommand.CreateFromTask(MyMethod2);
MyCommand3= ReactiveCommand.CreateFromTask(MyMethod3);
}
What I want is to make every command unexecutable when any other command is executing. I tried something like this:
public MyClass()
{
MyCommand1 = ReactiveCommand.CreateFromTask(MyMethod1, IsAnyCommandIsExecuting);
MyCommand2 = ReactiveCommand.CreateFromTask(MyMethod2, IsAnyCommandIsExecuting);
MyCommand3= ReactiveCommand.CreateFromTask(MyMethod3, IsAnyCommandIsExecuting);
}
private IObservable<bool> IsAnyCommandIsExecuting => this.WhenAnyObservable(
x => x.MyCommand11.IsExecuting,
x => x.MyCommand2.IsExecuting,
x => x.MyCommand3.IsExecuting);
But it didn't worked out: I had NotSupportedException
for some reason.
Question number 2 is how do I invert IsAnyCommandIsExecuting
. It is problematic since it has a type of Observable<bool>
. I didn't come up with something better that comparing with Observable.Return(false)
, but I feel like there has to be a better solution
Upvotes: 2
Views: 45
Reputation: 26
Try to do it like this
private IObservable<bool> IsAnyCommandIsExecuting => this.WhenAnyObservable(
x => x.MyCommand11.IsExecuting,
x => x.MyCommand2.IsExecuting,
x => x.MyCommand3.IsExecuting).Select(isExecuting => !isExecuting);
Upvotes: 1