RegionManager issues

Topics: Prism v2 - WPF 3.5
Feb 8, 2010 at 2:51 PM

Hi,

 

Im having some issues with the regionManager, possible my own misunderstandings but anoying.

I have a Shell view that creates a MainRegion (ContentControl), and a module that when intialized adds a view into the MainRegion that creates two new regions (both ContentControls).

My problem is that within my module controller I want to know which views are loaded into each of the localised regions (being ListViewRegion and ItemViewRegion), but whenever I attempt to get anything from regionManager.Regions[] I get an exception.

Here is a part of my module controller:

 

 private void Navigate(string pValue)
        {
            log.Info("Unsubscribing from navigation events");
            UnsubscribeNavigationEvents();
            
            // Show the main tasks window, this should do some detection first to see what we
            // currently have in the MainRegion
            ShowMainTasksRegion();

            GetActiveView(); //<-- this always throws an exception

            switch (pValue.ToUpper())
            {
                case "TASKLIST":
                    log.Info("Registering the TaskListView with the ListViewRegion");
                    _regionManager.RegisterViewWithRegion("ListViewRegion", () => _container.Resolve<TasksListView>());
                    break;
                case "TASKADD":
                    log.Info("Registering the TaskItemView with the ItemViewRegion");
                    _regionManager.RegisterViewWithRegion("ItemViewRegion", () => _container.Resolve<TaskItemView>());
                    break;
                case "RULELIST":
                    log.Info("Registering the RuleListView with the ListViewRegion");
                    _regionManager.RegisterViewWithRegion("ListViewRegion", () => _container.Resolve<RulesListView>());
                    break;
                case "RULEADD":
                    log.Info("Registering the RuleItemView with the ItemViewRegion");
                    _regionManager.RegisterViewWithRegion("ItemViewRegion", () => _container.Resolve<RuleItemView>());
                    break;
                default:
                    break;
            }

            log.Info("Subscribing to navigation events");
            SubscribeNavigationEvents();
        }

 

       private string GetActiveView()
        {
            IRegion detailsRegion = _regionManager.Regions[RegionNames.MainRegion];

            return "";
            
        }

Any pointers much appreciated. Mark

 

 

 

 

 

Feb 10, 2010 at 10:29 PM

Hi Mark,

The way you are trying to access the Region appears to be the right one, so the issue probably lays somewhere else.

One possible reason for this to happen could be if you are using Scoped Regions and try to access a region that was created in another RegionManager. To check this you can debug your application and check if the RegionManager instance that is used in the GetActiveView method is the same as the one were the MainRegion was created (or simply check if the “MainRegion” is in the Region’s collection of that Region Manager).

If the above is not helpful, please provide a small sample application so we can look deeper into this issue.

Please let me know if this helps.

Damian Schenkelman 
http://blogs.southworks.net/dschenkelman

Feb 11, 2010 at 7:12 AM

 

Hi Damien,

Unfortunately I gave up on this attempt, after changing my ContentControl regions to ItemsControl regions I had better luck albeit with a bit more work, heres a example of how i'm getting the views now:

 

 

switch (pValue.ToUpper())
            {
                case "TASKLIST":
                    log.Info("Registering the TaskListView with the ListViewRegion");
                    p_view = this._regionManager.Regions["ListViewRegion"].Views.FirstOrDefault(v => v.GetType() == typeof(RulesListView));
                    if (p_view != null)
                        this._regionManager.Regions["ListViewRegion"].Remove(p_view);
                    view = this._regionManager.Regions["ListViewRegion"].Views.FirstOrDefault(v => v.GetType() == typeof(TasksListView));
                    if (view == null)
                    {
                        view = this._container.Resolve<TasksListView>();
                        this._regionManager.Regions["ListViewRegion"].Add(view);
                    }
                    this._regionManager.Regions["ListViewRegion"].Activate(view);
                    break;

As you can see I have to do a bit of swapping in and out, but at least I can get the regions and their associated views now.

Thanks, Mark