[英]PRISM MEF Creating and using a new RegionManager
I don't have the code currently available, so I'll try to be as descriptive as possible. 我目前没有可用的代码,因此我将尽量描述性强。
I have a shell window with a menu. 我有一个带菜单的外壳窗口。 The menu has a menu item that opens a new modal dialog window with the shell window as it's parent. 该菜单具有一个菜单项,该菜单项打开一个新的模态对话框窗口,该窗口为父窗口。 I'm opening the dialog window using a custom interaction request and trigger action. 我正在使用自定义交互请求和触发操作打开对话框窗口。 The Content property of the custom INotification event takes a view (which is a window control in this case), and the view itself takes an IRegionManager as it's constructor. 自定义INotification事件的Content属性采用一个视图(在这种情况下为窗口控件),并且视图本身采用IRegionManager作为其构造函数。
[Import]
public IRegionManager { get; set; }
...{ Content = new DialogWindowView(regionManager.CreateRegionManager()) }
Then when the Action is called from the Shell view it gets a reference to the window from the Content property and calls content.ShowDialog()
然后,当从Shell视图调用Action时,它将从Content属性获取对窗口的引用并调用content.ShowDialog()
Then in the dialog window's constructor I'm adding some views to the dialog window's regions. 然后在对话框窗口的构造函数中,向对话框窗口的区域添加一些视图。
public IRegionManager RegionManager { get; set; }
public DialogWindowView(IRegionManager regionManager)
{
this.RegionManager = regionManager;
RegionManager.RegisterViewWithRegion("region1", typeof(view1));
RegionManager.RegisterViewWithRegion("region2", typeof(view2));
RegionManager.RegisterViewWithRegion("region3", typeof(view3));
}
In the dialog window's view I'm binding the regions to dialog window's region manager, like so: 在对话框窗口的视图中,我将区域绑定到对话框窗口的区域管理器,如下所示:
<ContentControl
prism:RegionManager.RegionManager="{Binding RegionManager, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
prism:RegionManager.RegionName="region1"/>
The dialog window loads and displays the views in it's regions, but when I close the dialog window and try to open it again it fails saying it can't add the views to the regions because they already exist. 对话框窗口会加载并显示其区域中的视图,但是当我关闭对话框窗口并再次尝试将其打开时,它无法显示无法将视图添加到区域中,因为它们已经存在。 I'm not sure how that's even possible since every time I open a dialog I'm calling new
on the dialog (so it should be a new dialog) and I'm passing in a brand new region manager (which should have no views associated with it). 我不确定这是怎么可能的,因为每次我打开对话框时,我都会在对话框上调用new
(因此应该是一个新对话框),并且要传入一个全新的区域经理(应该没有视图)与之关联)。 I've tried manually removing the views on the windows Closing event, and it appears to remove them, but then the dialog still won't reopen with an error that it cannot create the views. 我尝试过手动删除Windows关闭事件上的视图,但似乎已将其删除,但是对话框仍然不会因无法创建视图的错误而重新打开。
How do I properly open a new dialog window with it's own scoped region manager so that I can close it and reopen it? 如何使用它自己的作用域区域管理器正确打开一个新的对话框窗口,以便我可以关闭它并重新打开它?
I changed from view discovery to view injection and moved the code that adds the views to the region from the constructor to the loaded event. 我从视图发现更改为视图注入,并将将视图添加到区域的代码从构造函数移动到了加载事件。
RegionManager.Regions["region"].Add(ServiceLocator.Current.GetInstance<view>());
Without doing some more research I'm not 100% sure the difference between the two approaches, but it's working now, so that makes me happy. 如果不做进一步的研究,我不能100%地确定这两种方法之间的区别,但是它现在正在起作用,这让我很高兴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.