Tuesday, April 19, 2016

WhenActivated is called twice when used in Views and ViewModels hosted in ViewModelViewHost control

Leave a Comment

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?

1 Answers

Answers 1

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment