tags:

views:

929

answers:

1

Hi,

I have a WPF project based upon Prism Feb 2009 release set up as:

Shell exposes a single ContentControl as "MainRegion" Another view (user control) defined in the Infrastructure project called SplitView exposes two additional regions "LeftRegion" and "RightRegion" also as ContentControl.

Some of my application's modules need to display their view in the MainRegion (one user control), while others need to display their views (two user controls in a split fashion) in the LeftRegion and RightRegion.

I have tried using scoped regions, assuming that specific Controllers would hold references to the scoped regions. So basically each controller interested in SplitView functionality should instantiate a new SplitView (user control) and activate it in the MainRegion while activating its two user controls in the LeftRegion and RightRegion of the newly created scoped SplitView regions.

I am using MVVM with View Injection to display the views.

Needless to say, something has gone horrifically wrong with this approach.

At runtime I get this exception, "An exception occurred while creating a region with name 'LeftRegion'. The exception was: System.InvalidOperationException: Specified element is already the logical child of another element. Disconnect it first."

Am I correct at assuming that the LeftRegion and RightRegion are trying to register themselves with the main RegionManager every time I instantiate the SplitView?

Sorry about the confusing/verbose post. Any suggestions? Best practices to achieve this?

Thanks in advance,

Ali

A: 

The exception of "Specified element is already the logical child..." is what happens when you try to add something to two places in the tree, so I imagine there might be some logical error in your code, or you are adding something twice.

I generally create my sub regions like this:

    m_scopedRegionName = Guid.NewGuid().ToString(); /* EXAMPLE ! */
    m_scopedRegionManager =  m_regionManager.Regions[RegionNames.WORKSPACE_REGION].Add(myViewModel.View, m_scopedRegionName, true);
    m_someThingRegion = m_scopedRegionManager.Regions[RegionNames.SOME_THING_REGION];

Then I add any new stuff into the "m_someThingRegion".

Jeremiah Morrill
Jeremiah, I am doing something similar. Its just that in my case the "myViewModel.View" is being instantiated multiple times. Is that the mistake I am making? Are multiple insantiations allowed for a view that defines its own regions?
Ali
Sure you can have multiple instantiations for a view that contains regions, but in that case, they need to be added to their own scoped region. Make sure you are instantiating new views when you add to the region or else you will get that WPF exception.
Jeremiah Morrill
That was exactly the mistake I was making. I was not creating the additional view instances in their own scoped region. I basically had add the view to my MainRegion in a scoped region. Thanks for the clarification, the solution works fine now.
Ali