Switch between Views in different Modules using Prism and Unity

Topics: Prism v4 - WPF 4
Nov 12, 2012 at 2:12 PM
Edited Nov 12, 2012 at 2:25 PM

I am making a proof of concept WPF application with Prism 4 and Unity, but I run into some basic problems. 


In our solution we have the following projects:

-AppName.Desktop
-AppName.Modules.ModuleA
-AppName.Modules.ModuleB

Having followed some tutorials, explored some examples and searched the internet, I wasn't able to find a suitable answer to a very rudimentary question; how can I switch between two views in different DLL's.

The first view (ModuleAView) is loaded in the Initialize method of ModuleA:

 

public void Initialize() { 
regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.ModuleAView));}

 

When I click in ModuleAView (or on a button in ModuleAView) I want to switch to ModuleBView.       

 

private void SomeEventInModuleAView(object sender, RoutedEventArgs e) { 
	Uri viewNav = new Uri("ModuleBView", UriKind.Absolute);	regionManager.RequestNavigate(RegionNames.MainRegion, viewNav);� � 
}

Obviously this won't work, since ModuleA doesn't know where to find ModuleBView. I have read about changing the URI with pack/application/component etc, but I can't seem to make it work.    I was thinking something like this:       

 

Uri("pack://application:,,,/AppName.Modules.ModuleB;component/Views/ModuleBView.xaml", UriKind.Absolute);

Since loading modules from different assemblies is one of the purposes of Prism, I think it's strange that there aren't any examples in the Prism download to show how this works. 

Developer
Nov 12, 2012 at 4:57 PM

Hi,

Based on my understanding of your scenario, it seems that you are not registering the ModuleBView to be used with navigation.

Basically, when performing a navigation request in a region, Prism will try to construct the corresponding view through the dependency injection container of your application (in this case Unity) using the view's name passed in the Uri. Hence, if the view is not registered with that name in the dependency injection container, Prism would not be able to resolve it and the navigation request would fail.

In order for a view to be used with navigation in Unity you need to register it like this:

container.RegisterType<object,ViewClass>("ViewClass");

For example, you can register the ModuleBView like this:

// Module B
public void Initialize()
{ 
    container.RegisterType<object,ModuleBView>("ModuleBView");
}

Doing this, you can navigate to different views in different modules without needing for the modules to know each other or their assemblies.

You can find more information about this in the following chapter of the Prism documentation:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Nov 13, 2012 at 7:15 AM

Hi Damian,

That definitely did the trick! Thanks for this clear explanation.

I have bought the book, but I guess I need to shuffle the chapters a bit ;-)