Best way to move a view from one region to another?

Topics: Prism v4 - WPF 4
Feb 24, 2014 at 8:59 AM
What is the best way to move a view from one region to another? I want to keep the same View and not reload in another region.
Feb 24, 2014 at 4:06 PM
Based on my understanding, you would like to move an active View from one Region to another, and erase it from the first one.

Therefore, you could use a button to fire a moveView command and then, the commandHandler would remove the activeView from the first Region, and add it on the second one.
The CommandHandler implementation may look like this:
public void MoveViewCommandHandler()
    var viewToMove = this.regionManager.Regions["FirstRegion"].ActiveViews.FirstOrDefault();
    // Navigate on First region in order to load a View after the active View is moved
    this.regionManager.Regions["FirstRegion"].RequestNavigate(new Uri("MainDefaultView", UriKind.Relative)); 

     // Adding the View into the Second Region
     this.regionManager.Regions["SecondRegion"].RequestNavigate(New Uri("MyMovedView", UriKind.Relative));
If the View you would move is not active, then you would look for it in the Region's Views list and RequestNavigate() on the First Region would not be needed.

I hope this helps,

Gabriel Ostrowsky
Feb 25, 2014 at 8:59 AM
Thanks for replying!

Yeah this is great, just I am using the navigation journal. How is it best to correct the state of this? I only seem to have a clear on the journal rather than just remove the last entry?
Feb 25, 2014 at 4:06 PM
Hi james,

Based on my understanding, the clear method of the Navigation Journal would not completely help you as it would erase every View in the corresponding Region.
However, you would be able to create a custom RegionNavigationJournal for that region and define a method that would clear only the last entry and return it in order to add it on the other Region as you would need.

The new method could be implemented as follows:
public Entry RemoveLastEntry()
     var lastEntry = this.CurrentEntry;
     return lastEntry;
Notice that this new method would not be defined by IRegionNavigationJournal interface, so you could exclusively cast this RegionNavigationJournal to your custom NavigationJournal from the MoveViewCommandHandler() in order to make less changes as possible.
Finally, after removing the lastEntry from the first Region, you would be able to add the retreived entry into the second Region with RecordNavigation(lastEntry) method.

I hope you find this helpful.

Gabriel Ostrowsky
Feb 26, 2014 at 12:20 PM
Thanks, that sounds good!

Would it be possible for an example of this in action?
Feb 26, 2014 at 7:04 PM
Hi James,

You can find a small sample in the following SkyDrive link:

The App has a WindowView view which defines 2 Regions, MainRegion and SecondRegion. The MainRegion has HelloWorldView and View1 registered while the SecondRegion has View2 registered. Then, when clicking the button, the active View in MainRegion is moved to SecondRegion and activated. Therefore, View1 would be deactivated and removed from MainRegion and become active on SecondRegion.

Please notice that if the active view on MainRegion would not be the last one registered, the Journal's forwardStack would get empty but the Views would not be removed from the Region. You may see this in MainViewModel's MoveViewToRegion2 command handler.

I hope you find this helpful,

Gabriel Ostrowsky
Feb 28, 2014 at 8:58 AM
Sorry how do you do this using MEF?
Feb 28, 2014 at 2:38 PM
Hi James,

I believe you would find quite useful the following post regarding Unity to Mef migration and viceversa:

You would basically need to modify the Views registration and the way the dependencies are resolved through the container. Then, the solution approach would be quite similar to the one provided above.

You can notice on the PrismLibrary that MefRegionNavigationJournal, MefRegionNavigationService and MefRegionNavigationEntry inherit from the classes we modified and they doesn't add or overrides any other functionality.

I hope this helped,

Gabriel Ostrowsky