My app uses views, which implement IViewFor<T>
interface. The views are registered with the dependency resolver in AppBootstrapper
. The app displays the views using ViewModelViewHost
control by assigning a corresponding view model to control's ViewModel
property. All the view models implement ISupportsActivation
interface.
I noticed that WhenActivated
is called twice. First it's called when a view and view model get activated. Then on deactivation all disposables are disposed and WhenActivated
is called again immediately followed by disposing the disposables.
I am testing with the following code both in view and view model:
this.WhenActivated(disposables =>
{
Debug.WriteLine("ViewModel activated.");
Disposable
.Create(() =>
{
Debug.WriteLine("ViewModel deactivated.");
})
.AddTo(disposables);
});
As a result the output looks like this:
// 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 ViewModel property of ViewModelViewHost control to null.
Am I doing something wrong?
Edit: here's the complete source code: https://gist.github.com/dmakaroff/e7d84e06e0a48d7f5298eb6b7d6187d0
Pressing first Show and then Hide buttons produces the following output:
SubViewModel activated.
SubView activated.
SubViewModel deactivated.
SubView deactivated.
SubViewModel activated.
SubView activated.
SubViewModel deactivated.
SubView deactivated.
AppBootstrapper
, view and view-model classes (and any other classes in play)? – Cynde