Invoke module view load event

Topics: Prism v4 - WPF 4
Sep 1, 2011 at 2:54 AM


I came across an example showing how to dynamic inject views in 2 shells in ModuleView.xaml.cs:

        public MyShellWindow(IRegionManager regionManager)
            _regionManager = regionManager;
            Loaded += ShellDockWindow_Loaded;
void MyShellWindow_Loaded(object sender, System.Windows.RoutedEventArgs e) 
{ //code to use view injection to load a view into region on this Shell }
Usually, the load view code is located in ViewModel, but this is in the code of the View itself. Is it possible for the ViewModel to be aware of the "Loaded" event of the view, since
there may be other code in ViewModel that need to happen when View is loaded?
Sep 1, 2011 at 4:49 PM
Edited Sep 1, 2011 at 5:32 PM


There are various approaches that you can use to communicate between the View and the ViewModel to inform it that the view was loaded.

One of those approaches would be making the ViewModel subscribe to the "Loaded" event using the event aggregator so the View and the ViewModel would be still decoupled. This is important since the MVVM pattern usually implies that the ViewModel isn't aware of the specific implementation of the view related.

Another possible approach would be to create a shared service. When the Loaded event is raised, the View could use the service to inform the ViewModel that the View has been loaded.

For more information about this you can see the following chapter of the Prism documentation:

I hope you find this useful,

Damian Cherubini