为什么我的观点仅在第一次注入我的区域?

时间:2011-09-12 21:12:01

标签: wpf prism

我有一个MEF / Prism复合WPF应用程序,在以下情况下无法在区域中显示视图:

该应用有2个窗口。带有按钮的主启动窗口,当单击该按钮时,将创建并显示新窗口(子窗口)。子窗口有一些区域。当应用程序启动时,我使用RegionManager.RegisterViewWithRegion注册子窗口的视图。

现在,第一次创建子窗口并显示我注入的视图,它工作正常。但是,当我关闭子窗口然后单击主窗口上的按钮以创建并显示子窗口的新实例时,窗口已创建但没有注入视图。

就好像RegionManager已经丢失了注册,或者第二次无法在子窗口中找到区域。

知道为什么会这样工作吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

我有同样的问题(Unity / Prism 4)。为了解决这个问题,我从子窗口的区域中删除了活动视图,并在关闭子窗口之前删除了regionManager中的区域(OkButton和Cancel按钮)。 我不确定这是好的解决方案,但它确实有效。 (我的区域被添加到子窗口构造函数的区域管理器中)

子窗口构造函数

this.regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
RegionManager.SetRegionManager(this.ActionContent, this.regionManager);
RegionManager.SetRegionName(this.ActionContent, REGION_NAME);
this.regionManager.Regions[REGION_NAME].Add(this.sharedViewProperty.UIElement,
this.sharedViewProperty.ViewName);
this.regionManager.Regions[REGION_NAME].Activate(this.sharedViewProperty.UIElement);

OKButton_Click

this.regionManager.Regions[REGION_NAME].Deactivate(this.sharedViewProperty.UIElement);
this.regionManager.Regions.Remove(REGION_NAME);

现在,当我在构造函数中设置RegionManager时,我有一个奇怪的行为。 我有一个例外“Microsoft.Practices.Prism.Regions.Behaviors.RegionCreationException:创建名为'ChildRegion'的区域时发生异常。例外是:System.ArgumentException:具有给定名称的区域已经注册:”

当我在设置区域管理器之前插入以下这一行时,它可以工作:

this.regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

int nbCountRegion = this.regionManager.Regions.Count();

RegionManager.SetRegionManager(this.ActionContent, this.regionManager);
RegionManager.SetRegionName(this.ActionContent, REGION_NAME);

不要告诉我为什么,我无法解释它......