Loading and unloading of View

Topics: Prism v1, Prism v2 - WPF 3.5
May 28, 2010 at 6:15 AM
Edited May 28, 2010 at 9:08 AM

If we load a view from a module, then if we need to unloaded the view it would be a good practice to do it from that module only.

What would be the best approach to achieve  this?

ex:

 Initialize()

{

this.container.RegisterType<IMenuItemView, MenuItemView.MenuItemView>();
MenuItemViewPresenter presenter = this.container.Resolve<MenuItemViewPresenter>();
IRegion mainRegion = this.regionManager.Regions[Infrastructure.RegionNames.ManuRegion];
mainRegion.Add(presenter.View);

}

//OnClick is a event handler which keeps a watch on the view event.

OnClick(object sender, RoutedEventArgs e)

{

mainRegion.Remove(presenter.View);

}

 

My question: is it a good practice for a the model to keep a watch on its view event ?

Developer
Jun 1, 2010 at 7:53 PM
Edited Jun 1, 2010 at 7:53 PM

Hi,

It’s a good practice to make a module responsible for unloading a view it has loaded; but that doesn’t necessarily mean that the event that causes that view to be unloaded has to be published in the same module.

  • If the event is originated in another module, you could have the module subscribe to an event through the EventAggregator, which unloads the view when raised.
  • If the event is originated in the same module, it is ok to use RoutedEvents. As explained on Separated Presentation patterns, it isn’t a good practice for the model to keep a watch on its view’s event. However, you could have the Presenter (which would be called ViewModel if you are using the MVVM pattern) listen to that event and manage the unloading of the view.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi