ViewRegistrationException

An exception has occurred while trying to add a view to region 'MainNavigationRegion'. - The most likely causing exception was was: 'Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to
get instance of type ModuleAButtonView, key "" ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ModuleAButtonView, key "" at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type
serviceType, String key) at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) --- End of inner exception stack trace --- at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type
serviceType, String key) at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType) at Microsoft.Practices.Prism.Regions.RegionViewRegistry.CreateInstance(Type type) at Microsoft.Practices.Prism.Regions.RegionViewRegistry.<>c__DisplayClass1.<RegisterViewWithRegion>b__0()
at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(Object sender, ViewRegisteredEventArgs e)'. But also check the InnerExceptions for more detail or call .GetRootException().

The exception you are facing is being thrown because your view (ModuleAButtonView) is not registered within the container (in this case MEF) at the time you're trying to register it in a region.

One possible way to solve this would be to decorate your view with the
Exportattribute. By doing this your view will be registered within MEF, and then you could implement
View Discovery, for example like this:

This is necessary because internally, View Discovery uses the container (in your case MEF) to resolve the views that will be added to a region when they are created. You could check the
AutoPopulateRegionBehavior behavior for more information about this.

Another solution you might find useful to register your view using the View Discovery approach, would be to decorate it with
ViewExport attribute. Take into account in your ModuleAButtonView
class your comment this line, but not in ModuleAView class.

If you use ViewExport, your view is registered in the region that you're specifying (exactly like when you call the
RegisterViewWithRegion method), so there is no need to register the view into the region using the aforementioned method.

You might find the Prism Training Kit's MEF Hands-On lab useful, as it provides guidance on the use of the
ViewExport attribute.

Thanks but no, unfortunately it's not working either. I added ViewExport (and its dependent classes) but ToolbarView is not shown. However, if I remove RegionManager from ToolbarViewModel as I wrote in my previous post then it's showing the view.

Maybe the view is rejected by MEF? But I have looked in Mefx and it's not showing any rejections. But I'm not really sure I have done it right because I'm pretty new to MEF.

Note that the dependency to RegionManager is working in ModuleA class.

You could try changing the dependency to the RegionManager for a dependency to
IRegionManager, since an interface mapping is registered with the Region Manager (As you can see in the
MefRegionManager class, which exports the RegionManager with IRegionManager
as the contract type).