What is the difference between two RegisterViewWithRegion overloads in Prism

I use Prism4, and in one of my modules I try to register a view in a region, and also process its event with a button click (which is published when a user clicks a button on a view).

public class MyModule : IModule { private readonly IUnityContainer container; private readonly IRegionManager regionManager; private readonly IEventAggregator eventAggregator; public MyModule(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator) { this.container = container; this.regionManager = regionManager; this.eventAggregator = eventAggregator; eventAggregator.GetEvent<ViewAButtonClicked>().Subscribe(ViewAButtonClicked); } public void Initialize() { this.regionManager.RegisterViewWithRegion("MainRegion", typeof(ViewA)); // this 2nd overload would work fine //this.regionManager.RegisterViewWithRegion("MainRegion", () => this.container.Resolve<ViewA>()); } public void ViewAButtonClicked() { // some handling code // does *NOT* execute if using the 1st RegisterViewWithRegion overload // executes if using the 2nd RegisterViewWithRegion overload } } 

The code above works fine, except that it does not execute the ViewAButtonClicked method.

If I switched to using another overload (commented on the code above), then everything works as expected, the ButtonClicked method works.

The description from msdn is very similar, and I'm not sure why it gave me the other behavior described above. Why does one work and one does not work when it comes to a button click event?

RegisterViewWithRegion (IRegionManager, String, Func <(Of <(Object>)>)): Associate a view with a region using a delegate to resolve the concreate instance? Look. When a region is displayed, this deluxe will be called, and the result will be added to the region's collection of views.

RegisterViewWithRegion (IRegionManager, String, Type): Link the view to the region by registering the type. When a region is displayed, this type will be resolved using ServiceLocator in a specific instance. An instance will be added to the area view collection.

+4
source share
2 answers

The problem that you are likely to encounter is that when using the typeof method, the class of the GCed module, and thus the subscriber is not available.

When using a different approach, when you create a lambda to register a view in a region, the module class should be visible around.

The event aggregator has an option to save the link (without using a weak link), which should do the trick.

+3
source

Perhaps ViewA has dependencies that are not resolved when you do not retrieve them from the container?

0
source

All Articles