title: How to stop Caliburn Micro from closing the application when all views are deactivated?
body: <p>I am in the process of upgrading an old winforms program to WPF that utilizes Caliburn Micro for MVVM and Ninject for DI.</p>
  <p>I have updated my solution to a WPF solution which runs a non-generic implementation of the Caliburn Micro bootstrapper.  I do this because I do not currently have a base ShellViewModel to run from.</p>
  <p>I'm using a Ninject flavor of bootstrapper instead of the standard Caliburn Micro bootstrapper, since I'm fairly familiar with Ninject.</p>
  <p>The bootstrapper looks like this:</p>
  <pre><code>public class NinjectBootstrapper : Bootstrapper
  {
      private const string EXTENSION_PATH = &quot;Plugins&quot;;
      public static IKernel Kernel;
  
      protected override void Configure()
      {
          Kernel = new StandardKernel();
  
          Kernel.Bind&lt;IEventAggregator&gt;()
                .To&lt;EventAggregator&gt;()
                .InSingletonScope();
  
          Kernel.Bind&lt;ViewModels.IAnalysisOutputTableViewModel&gt;()
                .To&lt;ViewModels.AnalysisOutputTableViewModel&gt;()
                .InSingletonScope();
  
          Kernel.Bind&lt;IWindowManager&gt;()
              .To&lt;WindowManager&gt;()
              .InSingletonScope();
  
  
          Kernel.Bind&lt;FormMain&gt;()
                .ToSelf();
      }
  
      protected override object GetInstance(Type serviceType, string key)
      {
          if (serviceType != null)
          {
              return Kernel.Get(serviceType);
          }
  
          throw new ArgumentNullException(&quot;serviceType&quot;);
      }
  
      protected override IEnumerable&lt;object&gt; GetAllInstances(Type service)
      {
          return Kernel.GetAll(service);
      }
  
      protected override void BuildUp(object instance)
      {
          Kernel.Inject(instance);
      }
  }
  </code></pre>
  <p>To get my main form to startup I was forced to override OnStartup in my App.xaml.cs as such:</p>
  <pre><code>public partial class App : Application
  {
      protected override void OnStartup(StartupEventArgs e)
      {
          base.OnStartup(e);
          NinjectBootstrapper.Kernel.Get&lt;FormMain&gt;().Show();
      }
  }
  </code></pre>
  <p>As may be apparent above, I exposed my main Ninject kernel as a public static to get access to it, though I realize this is probably an anti-pattern, and once I have finished converting the program I will resolve this.</p>
  <p>I have implemented a new child window of my main form using proper MVVM.  I need to open the window from a button as such:</p>
  <pre><code>private void dataViewerButton_Click(object sender, EventArgs e)
  {
      windowManager.ShowWindow(analysisOutputTableViewModel);
  }
  </code></pre>
  <p>So far so good.</p>
  <p>The problem is when I try to close this child window.  Instead of just closing the window, it shuts down my entire program.</p>
  <p>I imagine this is because, since this child window is the only window(or view) Caliburn is handling, once it is deactivated Caliburn decides for me that I want to close the program.</p>
  <p>Is there any way to stop this automatic close behavior?  Perhaps a different way I should be initializing my main form?</p>
  <hr />
  <h3>Update</h3>
  <p>Using Marwijn's answer below, I was able to get my application closing properly.</p>
  <p>In the end I used <code>Application.Exit();</code> in my main form's Closing event:</p>
  <pre><code>private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
  {
      Application.Exit();
  }
  </code></pre>
  <p>in conjunction with the following modification to my App.xaml.cs:</p>
  <pre><code>public partial class App : Application
  {
      protected override void OnStartup(StartupEventArgs e)
      {
          base.OnStartup(e);
  
          var mainForm = NinjectBootstrapper.Kernel.Get&lt;FormMain&gt;();
          mainForm.Show();
  
          mainForm.FormClosed += mainForm_FormClosed;
      }
  
      void mainForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
      {
          this.Shutdown(0);
      }
  }
  </code></pre>
  <p>I can now load WPF windows from my main form using Caliburn Micro and Ninject, and everything shuts down properly and doesn't leave any processes running.  This solution appears like it will work well until I am able to update the main form to WPF.</p>