title: Initialize Shell with ViewModel after all modules loaded in Prism 6
body: <p>The ViewModel (set via <code>AutoWireViewModel="True"</code>) of my Shell / MainWindow requests a dependency which gets loaded in a module at startup using the <strong>ConfigurationModuleCatalog</strong>.<br>
  Because the Shell is initialzed before the modules, the DI container obviously can't resolve it, so the application crashes.</p>
  
  <pre><code>public class MainWindowViewModel : BindableBase
  {
      // Cannot resolve IService
      public MainWindowViewModel(IService service)
      {
      }
  }
  </code></pre>
  
  <p>I already tried the two approaches of <a href="https://stackoverflow.com/questions/7597917/is-it-possible-to-show-a-shell-only-once-all-the-modules-have-been-loaded"><strong>this post</strong></a>, but both didn't worked.  </p>
  
  <p>I tried it this way:</p>
  
  <pre><code>public interface IShellService
  {
      int NumberOfLoadedModules { get; }
  
      void FlagModuleAsLoaded();
  }
  
  public class ShellService : IShellService
  {
      private readonly IModuleCatalog _moduleCatalog;
  
      public ShellService(IModuleCatalog moduleCatalog)
      {
          _moduleCatalog = moduleCatalog;
      }
  
      public int NumberOfLoadedModules { get; private set; }
  
      public void FlagModuleAsLoaded()
      {
          NumberOfLoadedModules++;
  
          if (NumberOfLoadedModules != _moduleCatalog.Modules.Count())
              return;
  
          InitializeShell();
      }
  
      private static void InitializeShell()
      {
           Application.Current.MainWindow.Show();
      }
  }
  
  internal class Bootstrapper : UnityBootstrapper
  {
      protected override DependencyObject CreateShell()
      {
          return null;
      }
  
      protected override void InitializeShell()
      {
      }
  
      protected override void ConfigureContainer()
      {
          base.ConfigureContainer();
          Container.RegisterInstance&lt;IShellService&gt;(new ShellService(ModuleCatalog), new ContainerControlledLifetimeManager());
      }
  
      protected override IModuleCatalog CreateModuleCatalog()
      {
          return new ConfigurationModuleCatalog();
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Usage</strong></p>
  
  <pre><code>public abstract class ModuleBase : IModule
  {
      private readonly IShellService _shellService;
  
      protected ModuleBase(IShellService shellService)
      {
          _shellService = shellService;
      }
  
      public void Initialize()
      {
          InitializeInternal();
          FlagAsLoaded();
      }
  
      public abstract void InitializeInternal();
  
      public void FlagAsLoaded()
      {
          _shellService.FlagModuleAsLoaded();
      }
  }
  
  public class FooModule : ModuleBase
  {
      IUnityContainer _container;
  
      public MusicUIModule(IUnityContainer container, IShellService shellService) : base(shellService)
      {
          _container = container;
      }
  
      public override void InitializeInternal()
      {
          _container.RegisterType&lt;IService, Service&gt;();
      }
  }
  </code></pre>
  
  <p>It starts counting the modules and then the application crashes because of the same reason.<br>
  If the approach above isn't fitting for my purpose, how could that problem be solved?</p>
  
  <p>Thanks!</p>