ViewRegistrationException

Topics: Prism v4 - Silverlight 4
Dec 26, 2010 at 7:23 PM

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().

 

Below is the code where I am getting this exception

public class ModuleA : IModule
    {
        [Import]
        public IRegionManager RegionManager;

        public void Initialize()
        {
            this.RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationRegion, typeof(ModuleAButtonView)); ---->>>>>>>>>>>>>Exception is here
            this.RegionManager.RegisterViewWithRegion(RegionNames.MainContentRegion, typeof(ModuleAView));
        }
    }

 

////ButtonView

[PartCreationPolicy(CreationPolicy.Shared)] // creates a singleton instance of the view
    //[ViewExport (RegionName = RegionNames.MainNavigationRegion)] // registers the view with the MainRegion. More info: http://msdn.microsoft.com/en-us/library/ff921074(v=PandP.40).aspx    
    public partial class ModuleAButtonView : UserControl
    {

        [Import]
        public IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        private IRegionNavigationService navigationService;

        [ImportingConstructor]
        public ModuleAButtonView(IModuleManager moduleManager)
        {
            this.moduleManager = moduleManager;
            InitializeComponent();
        }
        private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
           
            //this.moduleManager.LoadModule("ModuleA");
            this.navigationService = regionManager.Regions[RegionNames.MainContentRegion].NavigationService;
           //string parameter= (e.OriginalSource as Button).CommandParameter as string;
            UriQuery query = new UriQuery();

              var uri = new Uri(ViewNames.Home + query.ToString(), UriKind.Relative);
             regionManager.RequestNavigate(RegionNames.MainContentRegion, uri);
        }
    }


///Module A View
[PartCreationPolicy(CreationPolicy.Shared)] // creates a singleton instance of the view
    [ViewExport (RegionName = RegionNames.MainContentRegion)] // registers the view with the MainRegion. More info: http://msdn.microsoft.com/en-us/library/ff921074(v=PandP.40).aspx    
    public partial class ModuleAView : UserControl, INavigationAware
    {
        public ModuleAView()
        {
            InitializeComponent();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            
        }
    }


//Viewnames
 public class ViewNames
    {
        public const string Home = "ModuleAView";
        public const string HomeNavItem = "ModuleAButtonView";
    }

Dec 27, 2010 at 3:14 PM

Hi,

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 Export attribute. By doing this your view will be registered within MEF, and then you could implement View Discovery, for example like this:
 

           this.RegionManager.RegisterViewWithRegion(RegionNames.MainNavigationRegion, typeof(ModuleAButtonView));

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.

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Jan 9, 2011 at 2:56 PM

I have the same problem, getting an exception when adding a view to region. My views are marked with ExportAttribute but that didn't solve the problem.

    [ModuleExport(typeof (ModuleA))]
    public class ModuleA : IModule
    {
        [Import] public IRegionManager RegionManager;

        #region IModule Members

        public void Initialize()
        {
            RegionManager.RegisterViewWithRegion("ToolbarRegion", typeof (ToolbarView)); <-------- Causing exception below
        }

        #endregion
    }
   
     [Export]
    public partial class ToolbarView : UserControl
    {
        [ImportingConstructor]
        public ToolbarView(ToolbarViewModel viewModel)
        {
            DataContext = viewModel;
            InitializeComponent();
        }

        public ToolbarView()
        {
            InitializeComponent();
        }
    }
   
     [Export]
    public class ToolbarViewModel : NotificationObject
    {
        private readonly RegionManager regionManager;

        [ImportingConstructor]
        public ToolbarViewModel(RegionManager regionManager)
        {
            this.regionManager = regionManager;
        }

        public ICommand ShowCreateSomethingCommand
        {
            get { return new DelegateCommand<string>(c => regionManager.RequestNavigate("MainRegion", new Uri(typeof(CreateSomethingView).Name, UriKind.Relative))); }
        }
    }

Microsoft.Practices.Prism.Regions.ViewRegistrationException was unhandled by user code
  Message=An exception has occurred while trying to add a view to region 'ToolbarRegion'.
    - The most likely causing exception was was: 'Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ToolbarView, key "" ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ToolbarView, 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().
  StackTrace:
       at Microsoft.Practices.Prism.Regions.RegionViewRegistry.OnContentRegistered(ViewRegisteredEventArgs e)
       at Microsoft.Practices.Prism.Regions.RegionViewRegistry.RegisterViewWithRegion(String regionName, Func`1 getContentDelegate)
       at Microsoft.Practices.Prism.Regions.RegionViewRegistry.RegisterViewWithRegion(String regionName, Type viewType)
       at Microsoft.Practices.Prism.Regions.RegionManagerExtensions.RegisterViewWithRegion(IRegionManager regionManager, String regionName, Type viewType)
       at Module.MyModule.MyModule.Initialize()
       at Microsoft.Practices.Prism.Modularity.ModuleInitializer.Initialize(ModuleInfo moduleInfo)
  InnerException: Microsoft.Practices.ServiceLocation.ActivationException
       Message=Activation error occured while trying to get instance of type ToolbarView, key ""
       StackTrace:
            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)
       InnerException: Microsoft.Practices.ServiceLocation.ActivationException
            Message=Activation error occured while trying to get instance of type ToolbarView, key ""
            StackTrace:
                 at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key)
                 at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
            InnerException:

However, if I remove the relation to RegionManager in my ViewModel then I don't get any exception. Like this.

    [Export]
    public class ToolbarViewModel : NotificationObject
    {
        //private readonly RegionManager regionManager;

        //[ImportingConstructor]
        //public ToolbarViewModel(RegionManager regionManager)
        //{
        //    this.regionManager = regionManager;
        //}

        //public ICommand ShowCreateSomethingCommand
        //{
        //    get { return new DelegateCommand<string>(c => regionManager.RequestNavigate("MainRegion", new Uri(typeof(CreateSomethingView).Name, UriKind.Relative))); }
        //}

        public ICommand ShowCreateSomethingCommand
        {
            get { return new DelegateCommand<string>(c => MessageBox.Show("hello")); }
        }
    }
   
I have tried to inject RegionManager by property as well but no luck.

Cheers,
Martin

Jan 9, 2011 at 3:43 PM

Hi Martin,

 

I am using following viewexport attribute to register the views and its working perfectly

  [ViewExport (RegionName = RegionNames.MainContentRegion)] // registers the view with the MainRegion. More info: http://msdn.microsoft.com/en-us/library/ff921074(v=PandP.40).aspx    
    public partial class ModuleAView : UserControl, INavigationAware
 

i hope this helps.

thnx
Jan 9, 2011 at 5:05 PM

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.


Strange.

 

 

Developer
Jan 10, 2011 at 4:02 PM
Edited Jan 10, 2011 at 4:03 PM

Hi Martin,

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).

[ImportingConstructor]
public ToolbarViewModel(IRegionManager regionManager)
{
      this.regionManager = regionManager;
}

I hope you find this helpful.

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

Jan 10, 2011 at 4:09 PM

Guido,

Yes, that was the problem. I just discovered it also. Thanks!