Prism: InvalidOperationException at region deactivation. "Deactivation is not possible in this type of region."

680 views Asked by At

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>
1

There are 1 answers

1
michalczukm On BEST ANSWER

Region Deactivate depends on implementation

Behaviour of Deactivate on IRegion 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 by ContentControl): only one active region at a time. It automatycly deactivate views at other view activation.
  • AllActiveRegion (set by ItemsControl): All views are visible and active. Calling Deactivate will cause InvalidOperationException.
  • Region (set by Selector): 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:

<Border Grid.Column="1" BorderBrush="#193441"  BorderThickness="2,2,2,2">
    <ContentControl regions:RegionManager.RegionName="ContentRegion" />
</Border>

And now my region is type of SingleActiveRegion where I don't have to call Deactivate.

When can i use Deactivate

  • When you have ContentControl and you want to deactivate only active view
  • If you want to keep multiple active views you have to use Selector control in .xaml - then you can use Deactivate