1

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();
  }
}
w3u37905
  • 37
  • 5
  • Why would want to instantiate a view before it is displayed? – mm8 Sep 13 '18 at 09:41
  • Because I use EventAggregator between view models to send messages and update data. – w3u37905 Sep 13 '18 at 11:19
  • So? This doesn't answer the question. A view is not initialized until it's displayed on the screen. – mm8 Sep 13 '18 at 11:25
  • But EditView is initialized without being displayed on the screen. The Data are not correct if view is not initialized. I should create new event to ask for the data. I am wondering if there is another solution. – w3u37905 Sep 13 '18 at 11:41
  • If you are creating data in the view you are doing this all wrong. The view should simply define the user interface. – mm8 Sep 13 '18 at 11:49
  • One view model sends to another presentation logic data such as the selected item in a listview. If a view and its viemodel are not instantiated will not receive these messages. – w3u37905 Sep 13 '18 at 12:18
  • 1
    Of course not. That's the nature of sending events through an event aggregator. You don't know if anyone will subscribe to the event you are sending - and you don't want to know. If you rely on all events being handled, you should probably consider using a shared service that you insantiate as a singleton in the bootstrapper. If a view model expects another view model to be alive when it sends a message, there is an indirect coupling between them after all. The purpose of using an event aggregator is to remove this tight coupling. – mm8 Sep 13 '18 at 12:26
  • hmm Interesting approach. Thanks for your help :) – w3u37905 Sep 13 '18 at 12:44

1 Answers1

1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88