开发者

Prism 4: Unloading view from Region?

开发者 https://www.devze.com 2023-02-21 16:37 出处:网络
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 module

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 co开发者_如何学JAVAntain 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.


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


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);
    }
}


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号