我正在使用 PRISM 5.0 开发 WPF 应用程序。
有时我想停用特定区域中的所有事件 View 。
IRegion contentRegion = _regionManager.Regions
.First(region => region.Name == RegionNames.ContentRegion);
foreach (object view in contentRegion.ActiveViews)
{
contentRegion.Deactivate(view);
}
但在这一点上我得到一个异常(exception):
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Deactivation is not possible in this type of region.
Source=Microsoft.Practices.Prism.Composition
StackTrace: ...
InnerException:
我的区域仅在基本 View Shell.xaml 中声明为
<Border Grid.Column="1" BorderBrush="#193441" BorderThickness="2,2,2,2">
<ContentControl regions:RegionManager.RegionName="ContentRegion" />
</Border>
最佳答案
区域停用取决于实现
IRegion
上 Deactivate
的行为取决于您在 xaml 中设置声明区域的实现。
它的实现由在 View 中设置的控件类型设置(此处为主视图 Shell.xaml)。
可能的实现以及如何设置它们:
SingleActiveRegion
(由ContentControl
设置):一次只有一个事件区域。它会在其他 View 激活时自动停用 View 。AllActiveRegion
(由ItemsControl
设置):所有 View 都可见且处于事件状态。调用Deactivate
将导致InvalidOperationException
。Region
(由Selector
设置):它允许多个事件和非事件 View 。
this post 中对其进行了广泛描述.
更改区域声明
对我来说,在这个区域只有一个事件 View 更舒服,所以我将 Shell.xaml
中的区域声明更改为:
<Border Grid.Column="1" BorderBrush="#193441" BorderThickness="2,2,2,2">
<ContentControl regions:RegionManager.RegionName="ContentRegion" />
</Border>
现在我的区域是 SingleActiveRegion
类型,我不必在其中调用 Deactivate
。
我什么时候可以使用Deactivate
- 当您有
ContentControl
并且您只想停用事件 View 时 - 如果您想保留多个事件 View ,您必须在 .xaml 中使用
Selector
控件 - 然后您可以使用Deactivate
关于c# - Prism:区域停用时出现 InvalidOperationException。 "Deactivation is not possible in this type of region.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31861311/