My app uses views, which implement IViewFor
interface. The views are registered with the dependency resolver in AppBootstrapper
. The app displ
The WhenActivated
call used in SubView
returns an IDisposable
object, which can be used within the same call to WhenActivated.
This will remove your subscription from activation events upon deactivation. Doing so prevents the secondary activation and disposal from occurring.
In the SubView
constructor, change this:
this.WhenActivated(d =>
{
Debug.WriteLine("SubView activated.");
d(Disposable.Create(() => { Debug.WriteLine("SubView deactivated."); }));
d(this // ViewModel -> DataContext
.WhenAnyValue(v => v.ViewModel)
.BindTo(this, v => v.DataContext));
});
to this:
System.IDisposable whenActivatedSubscription = null;
whenActivatedSubscription = this.WhenActivated(d =>
{
Debug.WriteLine("SubView activated.");
d(Disposable.Create(() => { Debug.WriteLine("SubView deactivated."); }));
d(this // ViewModel -> DataContext
.WhenAnyValue(v => v.ViewModel)
.BindTo(this, v => v.DataContext));
d(whenActivatedSubscription); // <- Dispose of the activation subscription here
});
The reason why this solution works is because since your view is being destroyed, the activation itself needs to be disposed of as a part of this process as well.
You can check this link. There is a very good explenation of everything. This is all I can offer without the knowledge of what you did in the "View" itself.
It is possible that the "ViewModel and View" are called twice because you are creating two LoadingViewModels.