I have a problem where I see events running after I expect all events to be completed. But that happens only when the method called is called by a ReactiveCommand.
I have a viewmodel that has one property and a calculated property:
public class ViewModel : ReactiveObject
{
private int _property;
private readonly ObservableAsPropertyHelper<int> _calculatedProperty;
public ViewModel()
{
Command = new DelegateCommand(x => Test("Command"));
ReactiveCommand = ReactiveCommand.Create(() => Test("ReactiveCommand"));
this.WhenAnyValue(x => x.Property)
.Do(x => Console.WriteLine($"CalculatedProperty will be set to {x}"))
.ToProperty(this, x => x.CalculatedProperty, out _calculatedProperty);
this.WhenAnyValue(x => x.CalculatedProperty)
.Subscribe(v => Console.WriteLine($"CalculatedProperty was set to {v}"));
}
public void Test(string method)
{
Console.WriteLine($"Begin {method}");
Property++;
Console.WriteLine($"End {method}");
}
public int Property
{
get => _property;
set => this.RaiseAndSetIfChanged(ref _property, value);
}
public int CalculatedProperty => _calculatedProperty.Value;
public ICommand Command { get; }
public ReactiveCommand ReactiveCommand { get; }
}
I have a window with 2 buttons, one bound to the Command and one to the ReactiveCommand (and one button with an oldfashioned clickhandler which behaves identical to the Command).
When I click the Command button, the outputted text is as I expect:
Begin Command
CalculatedProperty will be set to 1
CalculatedProperty was set to 1
End Command
However when I click the ReactiveCommand-button I get this:
Begin ReactiveCommand
CalculatedProperty will be set to 2
End ReactiveCommand
CalculatedProperty was set to 2
The calculated property event firing after the End-message is being run. This causes problems in my code and I do not understand why this happens. I've been investigating and playing with sending in schedulers in the ReactiveCommand.Create method, looking at SynchronizationContexts but all my experiments haven't worked out.
Why is this happening and what can I do to stop it?