May 4, 2011 at 2:28 PM
Edited May 4, 2011 at 2:28 PM
When you call the RegisterViewWithRegion(string regionName, Type viewType)
overload, the service locator is used to obtain an instance of the view type, and then the
RegisterViewWithRegion(string regionName, Func<object> getContentDelegate) overload is called, the
getContentDelegate pointing to a method that uses the service locator to retrieve the instance of the view type specified (as defined before). The region name, along with the
getContentDelegate are stored in a ListDictionary, which are then used to create the view when the region specified is added to the visual tree.
In the example you've mentioned, the delegate you're specifying contains a reference to a field of your
MyModule class, which is therefore kept alive. If you used the second overload like this:
this.regionManager.RegisterViewWithRegion("MainRegion", () => ServiceLocator.Current.GetInstance<ViewA>());
Your module wouldn't be kept alive.
However, you should take into account that the usual is to have your module class be garbage collected, and instead, subscribe to events and perform additional operations in a controller class, such as a ViewModel or a service.
As for which overload to use, the decision depends entirely on your personal preference and which one suits better your specific case.
I hope you find this helpful.
Guido Leandro Maliandi