Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism 4: Unloading view from Region?

Tags:

prism

prism-4

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.

like image 866
David Veeneman Avatar asked Apr 04 '11 02:04

David Veeneman


2 Answers

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

like image 93
David Veeneman Avatar answered Oct 13 '22 12:10

David Veeneman


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);
    }
}
like image 1
Vladislav Avatar answered Oct 13 '22 13:10

Vladislav