Search code examples
c#wpfprismprism-6

How to instantiate all the views in Prism


Bellow you can see my bootstrapper. I want to register all the views from the bootstrapper. When I start the application, WebView and EditView are created. GeneralView is a part of EditView and I have to navigate first to EditView in order to instantiate it. How can I instantiate all the views when starting the executable?

class Bootstrapper : UnityBootstrapper
{

  protected override DependencyObject CreateShell()
  {
    // Register views
    IRegionManager regionManager = this.Container.Resolve<IRegionManager>();


    regionManager.RegisterViewWithRegion("ContentRegion", typeof(WebView));
    regionManager.RegisterViewWithRegion("ContentRegion", typeof(EditView));

    // The following view is instantiated for the first time when I navigate to EditView
    regionManager.RegisterViewWithRegion("GeneralRegion", typeof(GeneralView));

    return Container.Resolve<MainWindow>();
  }

  protected override void InitializeShell()
  {
    Application.Current.MainWindow.Show();
  }

  protected override void InitializeModules()
  {
    base.InitializeModules();
  }
}

Solution

  • A view shouldn't be instantiated before it is actually being displayed on the screen. Besides, a view should only define the user interface.

    If you expect a specific view model to be alive when you are sending an event using the event aggregator from another view model, you are actually introducing an indirect coupling between these two view models. And this is exactly what you want to avoid by using an event aggregator in the first place.

    So if you rely on all events being handled, you should probably consider using a shared service that you instantiate as a singleton in the bootstrapper. You could then inject your view models with this shared service and communicate between them through the service interface.