棱镜区域 UpdateRegionsException

Prism Regions UpdateRegionsException

我有一个应用程序向区域管理器注册了 2 个区域,1 个是内容控件,另一个是自定义控件。 运行 应用程序时两者都很好,直到我尝试使用 RDP 会话。如果我从远程机器 运行 断开应用程序,然后重新连接 RDP 与剩余的应用程序 运行 我得到一个异常,自定义控件已经注册。两者都将 RegionMeneberLifetime 设置为 false。

第一个添加内容控件为

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

然后是自定义控件

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

自定义控件继承自ContentControl。

抛出的异常是

留言:

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

popupregion 似乎还没有被处理掉,在尝试再次添加它时它爆炸了。关于如何处理这个问题有什么建议吗?


找到解决方法。 视图不向区域管理器注册控件,而是在后面的代码中完成。

视图添加控件并为其命名

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

后面的代码在发生数据上下文更改事件时添加区域

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

一定要加

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

整个申请中只有一次。

如果您有 2 个同名的区域,您将遇到此异常。

(我没有足够的评论评论)