2

I have an application which registers 2 regions with the region manager, 1 is a content control and the other a custom control. Both are fine when running the application until I tried using an RDP session. If I disconnect from the remote machine running the application and then reconnect the RDP with the application left running I get an exception that the custom control is already registered. Both have the RegionMeneberLifetime set to false.

The content control is added 1st as

<ContentControl x:Name="MainRegion" Panel.ZIndex="0"
regions:RegionManager.RegionName="{x:Static sharedInterfaces:RegionNames.MainWorkspaceRegion}"
regions:RegionManager.RegionManager="{Binding RegionManager}"/>

and then the custom control

<controls:PopUpContainer regions:RegionManager.RegionName="{x:Static sharedInterfaces:RegionNames.PopupRegion}" 
                                 regions:RegionManager.RegionManager="{Binding RegionManager}"/>

The custom control inherits from ContentControl.

The exception thrown is

Message:

An exception occurred while creating a region with name 'MainWorkspaceRegion'. The exception was: Microsoft.Practices.Prism.Regions.UpdateRegionsException: An exception occurred while trying to create region objects. - The most likely causing exception was: 'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Practices.Prism.Regions.Behaviors.RegionCreationException: An exception occurred while creating a region with name 'PopupRegion'. The exception was: System.ArgumentException: Region with the given name is already registered: PopupRegion

It looks like the popupregion has not been disposed and in trying to add it again it blows up. Any suggestions on how I can handle this?

Soleil
  • 6,404
  • 5
  • 41
  • 61
DWatson
  • 41
  • 1
  • 6

2 Answers2

4

Be shure to add

regions:RegionManager.RegionName="{x:Static sharedInterfaces:RegionNames.PopupRegion}

just once in the whole Application.

If you've got 2 regions with same name, you will get this exception.

(I've got not enough reputation for comment)

Markus Weber
  • 1,059
  • 1
  • 11
  • 24
  • Thanks for your input but this is already in the code as you can see in the 2nd code snippet ' – DWatson Jan 14 '16 at 10:01
  • 1
    @DWatson I know, just thought maybe you didn't post all your code. There also may be other people (like me) who would be helped by this information. – Markus Weber Jan 14 '16 at 18:13
2

Found a work around. The view does not register the controls with the regions manager, instead it is done in the code behind.

The view adds the controls and gives them a name

<ContentControl x:Name="MainRegion" Panel.ZIndex="0"/>
<controls:PopUpContainer x:Name="PopupControl" Grid.ColumnSpan="2"/>

The code behind adds the regions when a datacontext change event occurs

private void ShellView_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var dataContext = DataContext as ShellViewModel;

    if (dataContext != null)
    {
        if (dataContext.RegionManager.Regions.ContainsRegionWithName(RegionNames.PopupRegion))
        {
            dataContext.RegionManager.Regions.Remove(RegionNames.PopupRegion);
        }

        RegionManager.SetRegionName(PopupControl, RegionNames.PopupRegion);
        RegionManager.SetRegionManager(PopupControl, dataContext.RegionManager);

        if (dataContext.RegionManager.Regions.ContainsRegionWithName(RegionNames.MainWorkspaceRegion))
        {
            dataContext.RegionManager.Regions.Remove(RegionNames.MainWorkspaceRegion);
        }

        RegionManager.SetRegionName(MainRegion, RegionNames.MainWorkspaceRegion);
        RegionManager.SetRegionManager(MainRegion, dataContext.RegionManager);
    }
}
Soleil
  • 6,404
  • 5
  • 41
  • 61
DWatson
  • 41
  • 1
  • 6