I'm developing an WPF application with PRISM 5.0.
At some point I want to deactivate all active views in specific region.
IRegion contentRegion = _regionManager.Regions
.First(region => region.Name == RegionNames.ContentRegion);
foreach (object view in contentRegion.ActiveViews)
{
contentRegion.Deactivate(view);
}
But at this point I get an 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:
My region is only declared at base view Shell.xaml as
<Border Grid.Column="1" BorderBrush="#193441" BorderThickness="2,2,2,2">
<ContentControl regions:RegionManager.RegionName="ContentRegion" />
</Border>
Region Deactivate depends on implementation
Behaviour of
Deactivate
onIRegion
depends on it's implementation that you set declaring region in xaml.Its implementation is set by type of control that is set at view (main view Shell.xaml here).
Possible implementations and how to set them:
SingleActiveRegion
(set byContentControl
): only one active region at a time. It automatycly deactivate views at other view activation.AllActiveRegion
(set byItemsControl
): All views are visible and active. CallingDeactivate
will causeInvalidOperationException
.Region
(set bySelector
): It allows multiple active and deactive views.It's widely described in this post.
Change region declaration
For me it is more comfortable to had only one active view in this region, so I've changed region declaration in
Shell.xaml
to:And now my region is type of
SingleActiveRegion
where I don't have to callDeactivate
.When can i use
Deactivate
ContentControl
and you want to deactivate only active viewSelector
control in .xaml - then you can useDeactivate