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?
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);
}
}