我正在使用PRISM 5.0开发WPF应用程序。
在某些时候,我想停用特定区域中的所有活动视图。
IRegion contentRegion = _regionManager.Regions
.First(region => region.Name == RegionNames.ContentRegion);
foreach (object view in contentRegion.ActiveViews)
{
contentRegion.Deactivate(view);
}
但此时我得到一个例外:
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:
我的区域仅在基础视图Shell.xaml中声明为
<Border Grid.Column="1" BorderBrush="#193441" BorderThickness="2,2,2,2">
<ContentControl regions:RegionManager.RegionName="ContentRegion" />
</Border>
答案 0 :(得分:5)
Deactivate
上IRegion
的行为取决于您在xaml中设置声明区域的实现。
它的实现是由在视图中设置的控件类型设置的(这里是主视图Shell.xaml)。
SingleActiveRegion
(由ContentControl
设置):一次只能有一个活动区域。它会在其他视图激活时自动停用视图。AllActiveRegion
(由ItemsControl
设置):所有视图均可见且有效。致电Deactivate
会导致InvalidOperationException
。Region
(由Selector
设置):它允许多个有效和无效视图。在this post中广泛描述。
对我来说,在这个区域只有一个活动视图更为舒服,所以我已将Shell.xaml
中的区域声明更改为:
<Border Grid.Column="1" BorderBrush="#193441" BorderThickness="2,2,2,2">
<ContentControl regions:RegionManager.RegionName="ContentRegion" />
</Border>
现在我所在的地区属于SingleActiveRegion
,我不必致电Deactivate
。
Deactivate
ContentControl
并且只想停用活动视图Selector
控件 - 然后您可以使用Deactivate