title: Managing Multiple Views/ViewModels In A Single ContentControl
body: <p>I have an application which shows a single View at a time in a ContentControl.  I have a current solution, but was curious if there is a better one for memory management.</p>
  
  <p>My current design creates new objects when they need to be displayed, and destroys them when they are no longer visible.  I'm curious if this is the better approach, or maintaining references to each view and swapping between those references is better?</p>
  
  <p>Here is a little more explanation of my application layout:</p>
  
  <p>A very simplified version of my MainWindow.xaml looks like this:</p>
  
  <pre><code>&lt;Window ... &gt;
    &lt;Window.Resources&gt;
      &lt;DataTemplate DataType="{x:Type vm:SplashViewModel}"&gt;
          &lt;view:SplashView /&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type vm:MediaPlayerViewModel}"&gt;
          &lt;view:MediaPlayerView /&gt;
      &lt;/DataTemplate&gt;
    &lt;/Window.Resources&gt;
    &lt;Grid&gt;
      &lt;ContentControl Content="{Binding ActiveModule}" /&gt;
    &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>In my MainViewModel.cs I swap the ActiveModule parameter with a newly initialized ViewModels.  For example, my pseudo-code logic check for swapping content would be something like:</p>
  
  <pre><code>if (logicCheck == "SlideShow")
    ActiveModule = new SlideShowViewModel();
  else if (logicCheck == "MediaPlayer")
    ActiveModule = new MediaPlayerViewModel();
  else
    ActiveModule = new SplashScreenViewModel();
  </code></pre>
  
  <p>But, would just maintaining a reference be more appropriate in speed and memory usage?</p>
  
  <p>Alt Option 1: Create static references to each ViewModel and swap between them...</p>
  
  <pre><code>private static ViewModelBase _slideShow = new SlideShowViewModel();
  private static ViewModelBase _mediaPlayer = new MediaPlayerViewModel();
  private static ViewModelBase _splashView = new SplashScreenViewModel();
  
  private void SwitchModule(string logicCheck) {
    if (logicCheck == "SlideShow")
      ActiveModule = _slideShow;
    else if (logicCheck == "MediaPlayer")
      ActiveModule = _mediaPlayer;
    else
      ActiveModule = _splashView;
  }
  </code></pre>
  
  <p>I'm not constantly creating/destroying here, but this approach appears to me to be wasteful of memory with unused modules just hanging out.  Or... is there something special WPF is doing behind the scenes that avoids this?</p>
  
  <p>Alt Option 2: Place each available module in the XAML and show/hide them there:</p>
  
  <pre><code>&lt;Window ... &gt;
    &lt;Grid&gt;
      &lt;view:SplashScreenView Visibility="Visible" /&gt;
      &lt;view:MediaPlayerView Visibility="Collapsed" /&gt;
      &lt;view:SlideShowView Visibility="Collapsed" /&gt;
    &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Again, I'm curious about what memory management might be happening in the background that I'm not familiar with.  When I collapse something, does it go fully into a sort of hibernation?  I've read that some stuff does (no hittesting, events, key inputs, focus, ...) but what about animations and other stuff?</p>
  
  <p>Thanks for any input!</p>