Question

I have tried to find an answer for the following scenario but have failed to come up with an answer. This may well be due to me, by my own admission, not yet having fully grasped the whole Prism concept. In my defense, I started looking into Prism only two days ago, so please be gentle...

After reading numerous literature I have the following setup:

  1. Framework Project
  2. Shell Project
  3. MainMenu project ( Module )
  4. SideMenu project ( Module )
  5. StatusBar project ( Module )
  6. TestContentA project ( Module )
  7. TestContentB project ( Module )

The Framework project contains, amongst other things, event definitions and payloads.

The Shell project has, within the shell.xaml, four regions:

  • MainMenuRegion
  • SideBarRegion
  • StatusBarRegion
  • ContentRegion

Every non-Shell project has references to the following Prism assemblies:

  • Microsoft.Practices.Prism
  • Microsoft.Practices.ServiceLocation
  • Microsoft.Practices.Unity

The Shell project has in addition to these assemblies, a reference to:

  • Microsoft.Practices.Prism.UnityExtensions

Every project has a reference to the Framework project, but none have a reference to any other project within the solution.

At this point I would like to point out that everthing works as planned!

In the MainMenuView project I have a few MenuItems which have commands in the MainMenuViewModel.

The ViewModel, using the EventAggregator, publishes a couple of Events declared in the Framework project.

In the Shell project, the ShellViewModel subscribes to these events, e.g.:

Using a ContentControl as the control for the "ContentRegion", I can switch between TestContentA and TestContentB with no problems. For the moment, both TestContentA and TestContentB are also registered using the

.RegisterViewWithRegion method(...)

What I would like to do is inject TestContentA and TestContentB into a TabControl as a new TabItem, instead of into the ContentControl control.

So shell.xaml now contains a TabControl along with a futher region called "TabRegion". I then changed the registered Region from ContentRegion to TabRegion, again, by using the

.RegisterViewWithRegion(...)

method.

I have seen a lot of examples like:

IRegion tabRegion = regionManager.Regions["TabRegion"];
var tabView1 = container.Resolve();
tabRegion.Add(tabView1, "FirstTabView");

I do not have the Container in the ShellViewModel but even if I did have it here, I still cannot refer to the TestContentA view, as this is in another assembly which itself will be found at runtime.

Unlike the samples you can find online, where the view which is to be injected always seems to be in the sample project.

In the Navigate method, if I (for debugging purposes) add the code:

var tabRegion = regionManager.Regions["TabRegion"];

then I can see that tabRegion has a View collection, and in this view I can see both views from TestContentA and TestContentB which tells me that the views were registered correctly. The problem is that I have no idea how to "grab" them so that I can inject a new instance of the view as a TabItem.

If I cheat and add a reference to TestContentA, then I can simply add the view and the TabItem will appear. This too tells me that I'm almost there but of course, no reference is wished.

To sum up, my question would be:

how can I inject a view from a loosely-coupled assembly which is discovered when the application starts up?

I would appreciate it if someone could point me in the right direction as I'm sure that it can be done.

Thank you for taking the time to read this!

Was it helpful?

Solution

You will need to create a bootstrapper. Each loaded module can get the region manager via ServiceLocator and than register its views to the region names. this way, the Shell project does not need to contain a direct reference to your views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top