The region manager does not contain the MainMenuRegion region.

Topics: Prism v4 - WPF 4
Jun 8, 2012 at 4:00 PM
Edited Jun 8, 2012 at 4:01 PM

In my shell defined the region( use DevExprss Control)

 

<dxd:DockLayoutManager x:Name="DemoDockContainer" Grid.Row="1" FloatingMode="Desktop"  MDIMergeStyle="Never" >
                    <dxd:LayoutGroup Name="Root" DestroyOnClosingChildren="False">
                        <dxd:LayoutPanel x:Name="panelToolbox" Caption="����" ItemWidth="200"  >
                            <dxn:NavBarControl >
                                <dxn:NavBarControl.View >
                                    <dxn:NavigationPaneView IsExpandButtonVisible="False"                                                            
                                                            dxn:ScrollingSettings.ScrollMode="ScrollBar" />
                                </dxn:NavBarControl.View>
                                <dxn:NavBarControl.Groups >
                                    <dxn:NavBarGroup Header="���˵�" DisplaySource="Content">
                                        <dxn:NavBarGroup.Content >
                                            <ContentControl Name="MainMenuContentControl"  prism:RegionManager.RegionName="{x:Static inf:RegionNames.MainMenuRegion}"/>
                                        </dxn:NavBarGroup.Content>

                                    </dxn:NavBarGroup>
                                    <dxn:NavBarGroup Header="�ҵ���Ϣ">

                                    </dxn:NavBarGroup>
                                </dxn:NavBarControl.Groups>
                            </dxn:NavBarControl>
                        </dxd:LayoutPanel>
                        <dxd:LayoutGroup Orientation="Vertical" >
                            <dxd:DocumentGroup MDIStyle="Tabbed" 
                                    prism:RegionManager.RegionName="{x:Static inf:RegionNames.DocumentRegion}">
                            </dxd:DocumentGroup>
                            <dxd:TabbedGroup ItemHeight="0.3*" DestroyOnClosingChildren="False"  prism:RegionManager.RegionName="{x:Static inf:RegionNames.TabRegion}">
                                
                            </dxd:TabbedGroup>
                        </dxd:LayoutGroup>
                    
                    </dxd:LayoutGroup>
                </dxd:DockLayoutManager>

 

And in module:

 

 [ModuleExport(typeof(Module))]
    public class Module:MesModule 
    {
     
        [ImportingConstructor]
        public Module(IRegionManager regionManager,
            IEventAggregator eventAggregator) :
            base(regionManager, eventAggregator)
        {
        }
                     

        protected override void RegisterViews()
        {
            base.RegisterViews();
          
            ServiceLoader.RegisterService<INavMainMenuView,INavMainMenuViewModel>("MainMenu");          
            ServiceLoader.LoadService("MainMenu",RegionNames.MainMenuRegion);
        }
               
    }

 

In ServiceLoader:

 

  public void LoadService(string mesService, string regionName, object parameter)
        {
            if (string.IsNullOrEmpty(regionName))
            {
                regionName = RegionNames.DocumentRegion;
            }
            ServiceDefinition serviceDefinition = services[mesService];
            if (!regionManager.ActivateViewIfExists(regionName, serviceDefinition.ViewInterfaceType))
            {
                regionManager.DeactivateViews(regionName);
                var viewModel = (IMesViewModel)ServiceLocator.Current.GetInstance(serviceDefinition.ViewModelInterfaceType);
                var view = (IMesView)ServiceLocator.Current.GetInstance(serviceDefinition.ViewInterfaceType);
                regionManager.AddViewToRegion(regionName, view);
          
                view.DataContext = viewModel;
                regionManager.ActivateViewIfExists(regionName, serviceDefinition.ViewInterfaceType);
                if (parameter == null)
                {
                    viewModel.Initialize();
                }
                else
                {
                   // viewModel.Initialize(parameter);
                }

            }
        }

 

In RegionManagerExtension:

 

  public static object GetView(this IRegionManager regionManager, string regionName, Type interfaceForView)
        {
            object view = null;
            regionManager.Regions[regionName].Views.ToList()
               .ForEach(aView =>
               {
                   if (interfaceForView.IsAssignableFrom(aView.GetType()))
                   {
                       view = aView;
                   }
               }
               );
            return view;
        }

        public static bool ActivateViewIfExists(this IRegionManager regionManager, string regionName, Type viewType)
        {
            bool returnValue = false;
            IMesView existingView = regionManager.GetView(regionName, viewType) as IMesView;
            if (existingView != null)
            {
                returnValue = true;
                regionManager.ActivateSingleView(regionName, existingView);
            }
            return returnValue;
        }

 

when run the code  will throw exception:The region manager does not contain the MainMenuRegion region.

I search the discussions, found  if change the code in module(mark blue) as below:

 

 RegionManager.RegisterViewWithRegion(RegionNames.MainMenuRegion, typeof(INavMainMenuView));

 

will run correctly. 

my problem is: is there are methods to still use the loaderService to Load the View?

Developer
Jun 8, 2012 at 5:41 PM

Hi,

Based on my understanding, I believe your problem seems to be very similar to the one described in the following DevExpress ticket, where an issue regarding Regions and DevExpress controls is reported:

In the aforementioned ticket you can find a sample application containing a custom implementation of the RegionManagerRegistrationBehavior created by the DevExpress Team that you might find useful to solve this problem.

Regards,

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