I'm developing a Blazor application using ReactiveUI and Mudblazor library. I have a ColorPickerViewModel with a SelectedColor property. In one of my components, I subscribe changes in this property to update the color of a card when the user selects a new color.
Subscription in the Component:
ViewModel.ColorPickerViewModel.WhenAnyValue(p => p.SelectedColor) .Skip(1) .SelectMany(async p => { var result = await ViewModel.CanvasViewModel.OnCardColorChanged(p); Snackbar.IfFail(result); await InvokeAsync(StateHasChanged); return Unit.Default; }) .Subscribe() .DisposeWith(_disposables);ColorPickerViewModel:public class ColorPickerViewModel : ReactiveObject, IColorPickerViewModel{ private string _color = ColorPalette.DomainEventColor; public string SelectedColor { get => _color; set => this.RaiseAndSetIfChanged(ref _color, value); } public void ApplyCardConfig(ReactiveBoardObjectBase card) { _color = card.Type; // Assigning directly to avoid triggering subscription }}Relevant Part of the ColorPicker Component:
public partial class ColorPicker : ReactiveInjectableComponentBase<IColorPickerViewModel>{ private MudColor _value = ColorPalette.DomainEventColor; [Inject] public required IToolBarViewModel ToolBarViewModel { get; set; } [Parameter] public required IEnumerable<MudColor> CustomPalette { get; set; } public MudColor SelectedValue { get => _value; set { _value = value; ViewModel!.SelectedColor = value.Value; } } protected override void OnInitialized() { _value = ViewModel!.SelectedColor; StateHasChanged(); }}Problem:The subscription to SelectedColor changes works most of the time, but it doesn't trigger when I select a second card and change its color to the same a changed in first card. Here's what happens:
I change the color of the first card to yellow.I select another card that has any color other than yellow.I try to change the color of this card to yellow, but the subscription doesn't trigger. In the ColorPicker component, I can see that it initially displays the correct color of card.
What I tried:
Replacing _color = card.Type; with SelectedColor = card.Type; in ApplyCardConfig solves the problem, but this triggers the subscription on card selection, which I don't want.Removing .Skip(1) from the subscription, but the issue persists.Adding .DistinctUntilChanged(), but it didn’t help.Question:Why doesn't the subscription trigger when I change SelectedColor to a different value after directly setting _color? How can I prevent the subscription from triggering on card selection but still have it work correctly when the user changes the color?
Any help or explanation would be greatly appreciated!