OnNavigatedFrom is not being called for views inside nested regions

Topics: Prism v4 - Silverlight 4
Apr 13, 2012 at 3:12 PM
Edited Apr 13, 2012 at 4:37 PM

I have an application in which each module has a MainView with nested region and its own menu structure, and other module views are shown in this nested region, all views have ViewModels that implements INavigationAware interface. The shell application has a menu that navigates between Modules Main Views. When i navigate to another Module view i want to clear ViewModels of the previous Module views, so i need to call the OnNavigateFrom method of the viewmodels of active views of the previous Module. I set ClearChildViewsRegionBehavior.ClearChildViews="True" for each MainView and when i navigate away i remove previous active MainView from RegionManager.Regions["ShellMainRegion"] and call

Microsoft.Practices.Prism.Regions.RegionManager.UpdateRegions();

to remove child views of the previous active module thinking this will call NavigateFrom methods of the view models of the active views, but this doesnt help too.

 What is the best pattern to clear view models of the previous active module?

 


Developer
Apr 13, 2012 at 6:12 PM

Hi,

Based on my understanding, when implementing the INavigationAware interface, the OnNavigatedFrom method of the corresponding view (or view model) will be called when a navigation request is performed on the region containing the aforementioned view. As the scoped views of the "main view" are hosted in scoped regions, the OnNavigatedFrom method of those view wouldn't be called as the navigation request was performed in the "main region," not in the scoped ones.

If after the navigation request is performed, the "main view" and its child views are removed from their corresponding regions (by specifying that the "main view" should not be kept alive when deactivated and using the ClearChildViewsRegionBehavior) and the reason you wish to call the OnNavigatedFrom method of the child views / view models is to execute some kind of "clean-up logic" before the they are disposed, a possible approach for this kind of scenario could be to implement the IDisposable pattern.

The .NET Framework provides an IDisposable interface which defines a Dispose method. As far as I know, this method would be automatically invoked before the object is garbage collected (in this case, the views or view models) allowing to execute "clean-up logic," for example, releasing resources. You can find more information about this in the following link:

If the aforementioned is not the case, I believe another possible approach could be to implement some custom logic in the OnNavigatedFrom method of the "main view" in order to retrieve all of its child elements and their corresponding DataContexts, filter them to find which ones implement the INavigationAware interface, and invoke their OnNavigatedFrom method manually.

Please let us know if we have misunderstood your scenario,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Apr 13, 2012 at 7:40 PM

Hi, thanks for your reply.

Yes, my aim is to clear some large collections when navigated away, for memory issues, i thought ClearChildViewsRegionBehavior may call OnNavigatedFrom method of active views viewmodels automaticaly. When navigated away from main view, the active views Unloaded event is called so i decided to make calls to OnNavigatedFrom method of the viewmodel in that event as you suggested in your reply last part. I dont need to call all viewmodels, becuase only the axctive view will need clearup, if not active it means it is never visited or OnNavigatedFrom is already called.

Unloaded += (s, e) =>{ ((INavigationAware)this.DataContext).OnNavigatedFrom(null); };

works.

 Regards.