Share the same instance of a viewmodel between two other viewmodels?

Topics: UI Architecture
Dec 5, 2012 at 11:08 AM

Consider that I have these view models:

  - ContentControl
  - ContentControl

Where A/BViewModel derive from Conductor<Screen>.Collection.AllActive and are different views of different viewmodels, except for one small area of common functionality, represented by CViewModel. When A/BViewModel get the instance of CViewModel, they set its Parent to themselves.

At the moment I have things working ok, as long as I configure my IoC container to provide unique instances of CViewModel.

I'd like to share the instance of CViewModel between A/BViewModel, such that when I switch between AViewModel and BViewModel (e.g. in the Shell) that CViewModel displays and behaves correctly.

If I set CViewModel to not be unique, or to be a singleton, when AViewModel is displayed I can see CViewModel, and then if I switch to BViewModel I can also see CViewModel (with already loaded data), but if I then switch back to AViewModel, the area where CViewModel should be is blank.

Am I doing something wrong, or is there a better way of handling this?