My application uses views that implement the interface IViewFor<T>. Views are registered using the dependency converter in AppBootstrapper. The application displays the views using the control ViewModelViewHostby assigning the appropriate view model to manage the property ViewModel. All view models implement the interface ISupportsActivation.
I noticed that it was WhenActivatedcalled twice. It is first called when the view and view model is activated. Then, when deactivating, all disposable items are located and WhenActivatedcalled again immediately, and then removes disposable items.
I am testing the following code in both the view and the model view:
this.WhenActivated(disposables =>
{
Debug.WriteLine("ViewModel activated.");
Disposable
.Create(() =>
{
Debug.WriteLine("ViewModel deactivated.");
})
.AddTo(disposables);
});
As a result, the output is as follows:
// App displays the view:
ViewModel activated.
View activated.
// App hides the view:
ViewModel deactivated.
View deactivated.
ViewModel activated.
View activated.
ViewModel deactivated.
View deactivated.
The view is hidden by setting the ViewModel property of the ViewModelViewHost control to null.
Am I doing something wrong?
Edit: full source code: https://gist.github.com/dmakaroff/e7d84e06e0a48d7f5298eb6b7d6187d0
Pressing the first button "Show" and "Hide" produces the following output:
SubViewModel activated.
SubView activated.
SubViewModel deactivated.
SubView deactivated.
SubViewModel activated.
SubView activated.
SubViewModel deactivated.
SubView deactivated.
source
share