c# - Prism:区域停用时出现 InvalidOperationException。 "Deactivation is not possible in this type of region."

标签 c# wpf prism prism-5

我正在使用 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>

最佳答案

区域停用取决于实现

IRegionDeactivate 的行为取决于您在 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/

相关文章:

.net - 是否可以在 XAML 中设置有选择地影响控件的样式?

MVVM - View 是否应该引用 Presenter/ViewModel?

android - Prism Xamarin.Forms monoandroid81 添加对“MonoAndroid”的引用

wpf - Prism:通过两个模块显示对话窗口

c# - 在 IIS6 中启用通配符映射时 WCF 方法返回 404

wpf - XAML 绑定(bind) BitmapImage ViewModel 属性

wpf - 无法更新 DataGrid

c# - 如何在Unity中测试方法包括异步方法

c# - 使用 C# 运行可执行文件并显示确认?

c# - docker build 期间 dotnet 发布失败