Application Initialization

Topics: Prism v4 - WPF 4
Nov 27, 2011 at 10:51 PM

I am building a prism application that has a TreeViewRegion on the left to handle views created by each module.  Each module add's thier own treeview to the TreeViewRegion.  When the application initializes and after all modules are loaded I want to select a default view in the treeview.  I have noticed that the views and the treeview does not display until after the bootstrapper.Run().  It seems the binding I am using gets called afterwards to set the collection in the treeview.  I thought I would be able to make the selection in the App.OnLoadCompleted method but noticed that it is never called.  How can I select a treeview item after the applicaton has initialized and created all it's default views?

Thanks

Developer
Nov 29, 2011 at 3:03 PM
Edited Nov 30, 2011 at 12:51 PM

Hi,

Based on my understanding you could raise an event at the end of the Bootstraper initialization sequence (once your Bootstrapper has finished Initializing all your modules). In your module, you can subscribe to this event and define the logic to select the view of your choice. For example you could call a RequestNavigate method to a specific view in your region.

To achieve this you could override the Bootstrapper.Run method and raise the event after calling the base.Run method, an example of this can be seen in the following code snippet:

 

public override void Run(bool runWithDefaultConfiguration)
{
base.Run(runWithDefaultConfiguration);

var eventAggregator = Container.Resolve<IEventAggregator>();

eventAggregator.GetEvent<ApplicationHasBeenInitializedEvent>().Publish();
}

 

Also you might find useful information in Chapter 2: Initializing Prism Applications of the Prism documentation at MSDN, where the different stages of the bootstrapping process is explained.

I hope you find this helpful.

Agustin Adami
http://blogs.southworks.net/aadami


Nov 30, 2011 at 4:03 AM

Hi,

As I mentioned in my initial post, I put a breakpoint at the end of the Bootstrapper.Run() method.  When the compiler breaks I notice that the shell and regions are created but the views are not shown in the regions.  The views have been created when the modules load and they are registered with RegisterViewWIthRegion but they are not shown in the regions at this point.  The debugger then steps into the CanExecuteChanged event in DelegateCommandBase then Commands.AddWeakReferenceHandler() then it steps into my viewmodel Items property that is bound to the treeviewview in the treeview region.  After this the views are shown in the regions.  Why does a call to eventAggregator as you have recoomend get called after the views are shown.  Why wouldn't it be called first? What is the ordering of when binding is completed versus a eventAggregator call?  In the meantime, I will try to implement what you said to see if makes a difference.

Thanks

Developer
Nov 30, 2011 at 1:14 PM

Hi,

If you can't find a solution to your problem after implementing the aforementioned suggestion, it would be helpful if you could provide us with a repro sample application, so that we can help you further with this.

Thanks,

Agustin Adami
http://blogs.southworks.net/aadami