Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current active view in a region using PRISM?

Tags:

c#

mvvm

wpf

prism

I know that i can get all the registered views in a region with :

var vs = mRegionManager.Regions[RegionNames.MainRegionStatic].Views.ToList();

and i can see there is the following code :

mRegionManager.Regions[RegionNames.MainRegionStatic].ActiveViews

which is giving a list of Active View, but I'm having my region attached to a ContentControl which always has a single ActiveView. Am i misunderstood or is there a way to get the single active view?

like image 795
Ehsan Zargar Ershadi Avatar asked Jun 15 '11 13:06

Ehsan Zargar Ershadi


3 Answers

var singleView = regionManager.Regions["MyRegion"].ActiveViews.FirstOrDefault();

like image 198
Navid Rahmani Avatar answered Nov 15 '22 03:11

Navid Rahmani


var singleView = regionManager.Regions["MyRegion"].ActiveViews.FirstOrDefault();

This is not correct, as it will just bring whatever view that got activated first. not the currently active/visible view.

Can't find a direct solution though, that doesn't involve custom implementation on View or ViewModel.

like image 41
Ahmed Avatar answered Nov 15 '22 02:11

Ahmed


Well, you could use the NavigationService Journal. It takes record of all the navigation that takes place in your application. So, you can get the name of the view like this:

string name = mRegionManager.Regions[RegionNames.MainRegionStatic].NavigationService.Journal.CurrentEntry.Uri;

Then you can get the view like this:

mRegionManager.Regions[RegionNames.MainRegionStatic].GetView(name);

Sweet Right? :)

like image 2
Prince Owen Avatar answered Nov 15 '22 01:11

Prince Owen