Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to inject a view into my shell. The docs state
Quote:
The RegionManager class provides access to the Region objects within the application.
but when I initialize the RegionManager class and attempt to call Regions["MAIN_REGION"] on this instance, it says no such region was found in the manager.

What I have tried:

I've also tried using a fresh Region instance altogether (since the docs infer no way to either access a global RegionManager or the one with knowledge of the regions in the shell).

Region v = new Region(); // also tried ServiceLocator.Current.GetInstance<RegionManager>();

v.RequestNavigate("MAIN_REGION", new Uri("Login", UriKind.Relative)); // Login is a valid code-behind while main_region is a prism region in my shell


The above does not display the region, presumably because it can find the view but not the region. Must I RegisterViewWithRegion when I don't intend to load only that view into my main_region all the time?
Posted
Updated 29-Jun-18 7:31am
v3

I do not work with regions myself but did you know that you can find a PRISM samples
project at:
GitHub - PrismLibrary/Prism-Samples-Wpf: Samples that demonstrate how to use various Prism features with WPF[^]

This project contains at least 2 Region specific samples (see below) - does this help answering your problem?

Prism-Samples-Wpf/02-Regions at master · PrismLibrary/Prism-Samples-Wpf · GitHub[^]

Prism-Samples-Wpf/03-CustomRegions at master · PrismLibrary/Prism-Samples-Wpf · GitHub[^]
 
Share this answer
 
Comments
nmeri17 29-Jun-18 10:19am    
Yes, I did know about them thanks. I've fixed it and injected the view but instead of displaying the view (which is a UserControl) in that region, it's converting it to a string and showing System.Object.
nmeri17 29-Jun-18 11:10am    
Also, if you don't use regions, how then do you display your views in the shell? I'm coming from the docs and online tutorials and I haven't seen one that doesn't use regions, except maybe the samples at msdn.
Dirk Bahle 29-Jun-18 11:14am    
You do not have to do the layouting via Regions just because you use PRISM. The normal layouting via XAML is still available - in my case I am using AvalonDock to the layouting for documents and tool windows which works well if you are interested in a document oriented application. You can review this project to understand how it works:
https://github.com/Dirkster99/Edi

(or follow my tutorial on AvalonDock if you are interested in that)
nmeri17 29-Jun-18 12:19pm    
Very well. Let me take a look, thanks.
nmeri17 29-Jun-18 12:59pm    
Hi, I eventually got through by looking at the following https://www.codeproject.com/Articles/165370/Creating-View-Switching-Applications-with-Prism and calling


IUnityContainer con = ServiceLocator.Current.GetInstance<IUnityContainer>();
con.RegisterType<Object, Login>("Login");

before invoking navigation. This worked over creating a new container like
IUnityContainer con = new UnityContainer();
con.RegisterType(typeof(Login), "Login");


Nevertheless, I'll still take a look at AvalonDock just for the fun of it, thanks again.
In order to obtain the region manager at work in the shell from other modules, I took the RegionManager argument passed into Shell by Prism, and assigned it to a member field.
public partial class Shell : Window
    {

        public IRegionManager x;

        public Shell(IRegionManager regionManager)
        {
            InitializeComponent();

            x = regionManager;
        }
    }


Next, in the Bootstrapper or wherever the module would be invoked from, grab that member field and pass it to your target module's constructor (which would be called before its initialize method).
protected override void InitializeShell()
        {
            Application.Current.MainWindow = (Shell) Shell;

            Application.Current.MainWindow.Show();

            IRegionManager u = ServiceLocator.Current.GetInstance<Shell>().x;

            IModule firstScreen = new LoginModule.LoginModule(u);


Finally, in the target module's constructor, you can use as desired.

    public void Initialize () {

        IUnityContainer con = ServiceLocator.Current.GetInstance<IUnityContainer>();

        con.RegisterType<Object, Login>("Login");

        Region mainRegion = (Region) _regionMan.Regions["MainRegion"]; // use here

        mainRegion.RequestNavigate(new Uri("Login", UriKind.Relative));
    }
}


Hope this helps someone else with the same problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900