Disconnecting a region

Topics: Prism v4 - WPF 4
Jun 23, 2011 at 6:27 PM

Hi there,

I've the Window shell and I've created a new window that is shown as a Dialog. This new window has its own region named "WizardStepsRegion" and I inject some views inside it.

The first time I click the button that shows the Dialog it works as expected, but the second time I get the following exception:

RegionCreationException:
An exception occurred while creating a region with name 'WizardStepsRegion'. The exception was: System.InvalidOperationException: Specified element is already the logical child of another element. Disconnect it first.

I don't understand why I get such exception because I'm creating a new RegionManager to add the regions of the Dialog and I get a new fresh instance every time I show the dialog.

Can somebody explain me what's wrong please?

Here's the code

Wizard.xaml

<DockPanel LastChildFill="True"> 
 <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" DockPanel.Dock="Bottom"> 
  <Button Content="_Previous" Margin="0, 0, 10, 0" /> 
  <Button Content="_Next" /> 
 </StackPanel> 
 <ContentControl cal:RegionManager.RegionName="WizardStepsRegion" />
</DockPanel>


Wizard.xaml.cs

[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
//[RegionMemberLifetime(KeepAlive = false)]
public partial class Wizard : Window
{ 
  public IRegionManager regionManager; 
  RegionManager regMan = new RegionManager();

  [ImportingConstructor] 
  public Wizard(IRegionManager rm) 
  { 
    InitializeComponent(); 
    regionManager = rm; 
    RegionManager.SetRegionManager(this, regMan); 
    regMan.RegisterViewWithRegion("WizardStepsRegion", typeof(Step1View)); 
  }

  private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
  {
    foreach (object v in regMan.Regions["WizardStepsRegion"].Views)
    {
      regMan.Regions["WizardStepsRegion"].Remove(v); 
    } 
  }

 

Here's the code that create and show the dialog

Wizard wizard = (Wizard)ServiceLocator.Current.GetInstance(typeof(Wizard));
wizard.ShowDialog();


Thank you

Developer
Jun 23, 2011 at 7:56 PM

Hi,

It would be helpful if you could provide us with a repro sample application, so that we can help you find the cause for the issue you're experiencing.

Thanks,

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