RegisterViewWithRegion After Module Is Loaded

Oct 23, 2009 at 9:16 PM

Hey All,

From what I gather the general pattern to add an instance of a view to a region is to call RegisterViewWithRegion in the Initialize() method of the corresponding module like so

        public void Initialize()
        {
            this._RegionManager.RegisterViewWithRegion("MainRegion", typeof(MyControl));
        }

However what if you want to add an instance of a view to a region sometime after the module is loaded? How would you accomplish that?

I'm not using a presenter-first pattern but rather a Screen Factory / Conductor / whatever its called to create and orchestrate views. Given that I shouldn't be using RegisterViewWithRegion in this way anyway because then view creation falls outside of the bounds of the Screen Factory. However the overload of RegisterViewWithRegion using a delegate would necessitate that my Screen Factory have a hard reference to the view being loaded which I don't desire.

Anybody else run into this problem?

Oct 23, 2009 at 9:41 PM

Can you use View Injection?

 

Something like:

IRegion reg = regionManager.Regions["SomeRegion"];
View vw = ; // <<< construct you view >>>
reg.Add(vw, "MyViewName");

// <<< optional >>>
reg.Activate(vw); // if the region is a tab control, this is supposed to give it the focus

 

Oct 23, 2009 at 9:44 PM
cmichaelgraham wrote:

Can you use View Injection?

 

Something like:

 

IRegion reg = regionManager.Regions["SomeRegion"];
View vw = ; // <<< construct you view >>>
reg.Add(vw, "MyViewName");

// <<< optional >>>
reg.Activate(vw); // if the region is a tab control, this is supposed to give it the focus

 

 

How do you get access to the type of the view? If I put this code in my Screen Factory (which is in the infrastructure project), the type "View" doesn't exist. Do I create an instance of the view with reflection or something?

 

Oct 23, 2009 at 11:43 PM

The way I understand the ScreenFactory pattern is that you need one for every MVVM triad, the factory knows about the View the Model and the ViewModel to be able to create them, for this to work the factory needs to reside in the same module as the triad.

Oct 24, 2009 at 5:09 PM

Ahhh okay that makes sense. But then would you additionally need an overall "Conductor" at a higher level to direct the individual Screen Factories to create and destroy individual views?

Oct 26, 2009 at 3:32 PM

Yes, you only need one Screen Conductor for the whole application.