Prism 4:从区域卸载视图?

发布于 2024-10-29 00:23:04 字数 830 浏览 1 评论 0原文

如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素食主义者 2024-11-05 00:23:04

我找到了答案,尽管我不能说我完全理解它。我使用 IRegionManager.RequestNavigate() 将 RibbonGroup 注入到功能区的“主页”选项卡中,如下所示:

// Load RibbonGroup into Navigator pane
var noteListNavigator = new Uri("NoteListRibbonGroup", UriKind.Relative);
regionManager.RequestNavigate("RibbonHomeTabRegion", noteListNavigator);

我更改了代码以通过将视图注册到区域来注入视图,如下所示:

// Load Ribbon Group into Home tab
regionManager.RegisterViewWithRegion("RibbonHomeTabRegion", typeof(NoteListRibbonGroup));

代码删除 RibbonGroup:

if(ribbonHomeTabRegion.Views.Contains(this))
{
    ribbonHomeTabRegion.Remove(this);
}

现在我可以使用以下 ,如何注入视图显然很重要。如果您希望能够删除视图,请通过向区域管理器注册来注入

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:

// Load RibbonGroup into Navigator pane
var noteListNavigator = new Uri("NoteListRibbonGroup", UriKind.Relative);
regionManager.RequestNavigate("RibbonHomeTabRegion", noteListNavigator);

I changed the code to inject the view by Registering it with the region, like this:

// Load Ribbon Group into Home tab
regionManager.RegisterViewWithRegion("RibbonHomeTabRegion", typeof(NoteListRibbonGroup));

Now I can remove the RibbonGroup using this code:

if(ribbonHomeTabRegion.Views.Contains(this))
{
    ribbonHomeTabRegion.Remove(this);
}

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

酒浓于脸红 2024-11-05 00:23:04

Microsoft 的 StockTraderRI 示例项目包含以下从 ViewModel 中的区域中删除视图的示例。

private void RemoveOrdersView()
{
    IRegion region = this._regionManager.Regions[RegionNames.ActionRegion];

    object ordersView = region.GetView("OrdersView");
    if (ordersView != null)
    {
        region.Remove(ordersView);
    }
}

StockTraderRI Example Project by Microsoft contains the following example of removing views from region in ViewModel.

private void RemoveOrdersView()
{
    IRegion region = this._regionManager.Regions[RegionNames.ActionRegion];

    object ordersView = region.GetView("OrdersView");
    if (ordersView != null)
    {
        region.Remove(ordersView);
    }
}
拥抱没勇气 2024-11-05 00:23:04

您是否有可能有一个 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文