Extra Viewmodel deactivation using SimpleMDI example.

Topics: Getting Started, UI Architecture
Dec 20, 2012 at 12:34 PM


I'm reasonably sure there's simple explanation for this behaviour that's just passed me by.

Using the SimpleMDI as a starting point to create a simple example, I've made the TabViewModel a OneActive screen Conductor (I intend using it to manage multiple screens appearing on a single tab).

In this simple example I've then added a UserControl "TestView" with a corresponding "TestViewModel" that is Activated in the TabViewModel constructor and resides in a ContentControl on the TabView.

I'm writing to the debug window whenever "TestViewModel" has the OnDeactivate method called.

Every time I create a new tab, the "TestViewModel" OnDeactivate method fires, which I'm expecting because the tab it resided is now inactive. The same thing happens when I switch tabs, as each new tab and associated "TestViewModel" becomes active, the previous one is deactivated.

the behaviour I'm having trouble understanding occurs when the application is closed, I see additional OnDeactivate methods fired for each of the tabs I have open, I would expect to see only one, since the others are deactivated when I navigate away from them.

I have some inkling that I'm doing something incorrectly, or that I'm not entirely understanding what I'm doing in this example; I'm potentially misusing the Conductor, or not accounting for a ViewModel somewhere.

I can include my simple example if needed, but I'm sure it's just a daft question. Thanks for any help.


Dec 20, 2012 at 1:41 PM

OnDeactivate has a bool parameter called "close".
If only another tab gets activated the previous get deactivated (close=false).
If you close a tab OnDeactivated() is called but with close=true, so you can do some cleanup.

Dec 20, 2012 at 2:19 PM

Ah, thanks!

I had tried altering my example slightly to include a manual call to OneActive.DeactivateItem(item, true) which was giving me the result I was expecting, but I wasn't convinced I understood why.

For some reason my brain had assumed that OnDeactivate would only be called from items that were "Active", my assumption was that all my deactivated usercontrols would be closed, but their OnDeactivate methods wouldn't fire.

Thanks for clearing that up.