Prism 4:从区域卸载视图?
如何从 Prism 区域卸载视图?
我正在编写一个 WPF Prism 应用程序,并在 Shell 中使用 Ribbon 控件。功能区的“主页”选项卡包含一个区域 RibbonHomeTabRegion
,我的一个模块(称为 ModuleA)将一个 RibbonGroup
加载到该区域中。效果很好。
当用户离开 ModuleA 时,需要从 RibbonHomeTabRegion 卸载 RibbonGroup
。我不会用另一个视图替换RibbonGroup
——该区域应该是空的。
编辑:我重写了问题的这一部分:
当我尝试删除视图时,收到一条错误消息“该区域不包含指定的视图”。因此,我编写了以下代码来删除该区域中的任何视图:
// Get the regions views
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
var ribbonHomeTabRegion = regionManager.Regions["RibbonHomeTabRegion"];
var views = ribbonHomeTabRegion.Views;
// Unload the views
foreach (var view in views)
{
ribbonHomeTabRegion.Remove(view);
}
我仍然收到相同的错误,这告诉我有一些非常基本的事情我做错了。
有人能指出我正确的方向吗?感谢您的帮助。
How do I unload a view from a Prism Region?
I am writing a WPF Prism app with a Ribbon control in the Shell. The Ribbon's Home tab contains a region, RibbonHomeTabRegion
, into which one of my modules (call it ModuleA) loads a RibbonGroup
. That works fine.
When the user navigates away from ModuleA, the RibbonGroup
needs to be unloaded from the RibbonHomeTabRegion
. I am not replacing the RibbonGroup
with another view--the region should be empty.
EDIT: I have rewritten this part of the question:
When I try to remove the view, I get an error message that "The region does not contain the specified view." So, I wrote the following code to delete whatever view is in the region:
// Get the regions views
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
var ribbonHomeTabRegion = regionManager.Regions["RibbonHomeTabRegion"];
var views = ribbonHomeTabRegion.Views;
// Unload the views
foreach (var view in views)
{
ribbonHomeTabRegion.Remove(view);
}
I am still getting the same error, which tells me there is something pretty basic that I am doing incorrectly.
Can anyone point me in the right direction? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了答案,尽管我不能说我完全理解它。我使用 IRegionManager.RequestNavigate() 将 RibbonGroup 注入到功能区的“主页”选项卡中,如下所示:
我更改了代码以通过将视图注册到区域来注入视图,如下所示:
代码删除 RibbonGroup:
现在我可以使用以下 ,如何注入视图显然很重要。如果您希望能够删除视图,请通过向区域管理器注册来注入
I found my answer, although I can't say I fully understand it. I had used IRegionManager.RequestNavigate() to inject the RibbonGroup into the Ribbon's Home tab, like this:
I changed the code to inject the view by Registering it with the region, like this:
Now I can remove the RibbonGroup using this code:
So, how you inject the view apparently matters. If you want to be able to remove the view, inject by registration with the Region Manager
Microsoft 的 StockTraderRI 示例项目包含以下从 ViewModel 中的区域中删除视图的示例。
StockTraderRI Example Project by Microsoft contains the following example of removing views from region in ViewModel.
您是否有可能有一个 RegionAdapter 在添加视图之前将视图包装在另一个视图中? RibbonHomeTabRegion 应该有一个带有视图集合的属性——里面有什么东西吗?
Is it possible you have a RegionAdapter that is wrapping the view inside another view before adding it? The
ribbonHomeTabRegion
should have a property with the collection of views - is there anything inside it?