How to get a name of the active view in PRISM
Asked Answered
L

2

0

I am trying to navigate to the same view (same as the active one), but with new parameters. The problem is that I struggle to find a clean way to get a currently active view's name from RegionManager

Latrinalatrine answered 9/9, 2013 at 13:26 Comment(6)
The IRegion has an ActiveViews property that will give you a read-only collection of all currently active views. Have you tried that?Labradorite
@BrianS This collection inherits from IEnumerable<Object> which only has an actual view object, but not it's names. In other words active view doesn't provide any way to look up the actual name of the view.Latrinalatrine
I'm not clear on the design, and why you can't determine what view it is from the object. Can you just create an IMyView interface with a Name property that all your views implement, then cast the object to IMyView and get the name of the view?Labradorite
It sounds like you may have multiple instances of the same view in a Region, each with different parameters that differentiate it from the others. Is that correct? An you want to know which instance is currently active?Labradorite
Yes, I have considered this option, but it's not great that you have to implement separate interface just in order to get round PRISM not exposing some of the private fields. I hope there is a cleaner way of doing thatLatrinalatrine
What do you mean PRISM not exposing the private fields? Private members, by their nature, are not exposed publicly - this is not a PRISM thing. If you have an external component that needs to discover something about your object, it needs to be either public or internal (if the component is within the same assembly). Using an interface to surface public properties is actually good practice.Labradorite
L
4

From my understanding, I would recommend implementing an interface on each of your Views that provides the necessary information to determine which view it is, then use the ActiveViews property of the IRegion to access the active views.

Something like:

//Interface for identifying the Views
public interface IApplicationView
{
    string ViewName { get; }
}

//Example of a View with logic to determine which instance of the View it is
public class ApplicationView : UserControl, IApplicationView
{
    public string ViewName 
    { 
        get { return this.isViewA ? return "ViewA" : return "ViewB"; } 
    }
}

//Example of determining which view
public class OtherComponent
{
    public string GetActiveViewName(IRegion region)
    {
        IApplicationView activeView = region.ActiveViews.FirstOrDefault() as IApplicationView;
        return activeView == null ? string.Empty : activeView.ViewName;
    }
}
Labradorite answered 9/9, 2013 at 14:44 Comment(2)
As fas as I can see there isn't a clean way to do it, so method above solves the problem.Latrinalatrine
Is there a way to get existing ViewModels using the IoC container (in my case DryIoc)? I tried this == _Container.Resolve<ShellViewModel> from the ShellViewModel and it evaluated to false.Neoplasty
V
4

I'm not sure if this method is implemented in Prism 4. But in Prism 6, you can get the name of the active view using the NavigationService Journal.

For example:

string name = RegionManager.Regions["MainRegion"].NavigationService.Journal.CurrentEntry.Uri;
Venettavenezia answered 5/6, 2018 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.