I have MAUI application with ReactiveUI.MAUI
and ReactiveUI.SourceGenerators
packages.
Here is view model:
public partial class VM: ReactiveObject{ [Reactive] public partial string? Property { get; set; }}
View should monitor for property changes and do something. Tricky part is that view will be loaded/unloaded when navigating to/from certain FlyoutItem
, containing view. Unsubscribing therefore is important.
Showing view first time - everything works. Unloading and then loading view again - subscribing doesn't work anymore.
Here is working code (works all the time):
VM? vm;public View(){ Loaded += (s, e) => { vm = BindingContext as VM; vm?.PropertyChanged += VM_PropertyChanged; }; Unloaded += (s, e) => { vm?.PropertyChanged -= VM_PropertyChanged; };}void VM_PropertyChanged(object? sender, PropertyChangedEventArgs e){ ... // breakpoint here is always hit}
Here is not working code, subscriber is not detecting property changes after unloading and loading view again:
readonly CompositeDisposable disposables = [];public View(){ Loaded += (s, e) => Init(); Unloaded += (s, e) => disposables.Dispose();}void Init(){ if (BindingContext is not VM vm) return; vm.WhenAnyValue(o => o.Property) .Subscribe(property => { // problem: not triggered on property changes after re-loading view }) .DisposeWith(disposables);}
Why in reactive approach the property changes are not detected anymore after unloading the view and loading it again?
The Load
/Unload
events are working like clocks (checked with breakpoints). The subscriber is triggered once when Init
is called as expected.
VM instance doesn't change if that matter.