I am creating a application with Prism and Unity. I use the DirectoryModuleCatalog
to load several modules from disk which are displayed in a main menu and when you click on the name of this particular module the UI of this module gets loaded.
Each module is designed according to the MVVM model, so with a separate view and view model.
Bootstrapper:
class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
Shell shell = Container.Resolve<Shell>();
shell.Show();
return shell;
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)this.Shell;
App.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType<IApplicationMenuRegistry, MenuRegistry>();
Container.RegisterType<IApplicationCommands, ApplicationCommands>();
Container.RegisterType<ShellViewModel, ShellViewModel>(new Microsoft.Practices.Unity.ContainerControlledLifetimeManager());
//****** When I uncomment following line, the HelloWorldModule2 doesn't get initialized ***********
// Container.RegisterType<HelloWorldModule2ViewModel, HelloWorldModule2ViewModel>(new Microsoft.Practices.Unity.ContainerControlledLifetimeManager());
}
protected override IModuleCatalog CreateModuleCatalog()
{
return new DirectoryModuleCatalog() { ModulePath = @"C:\Data\NPC Service Tool\Source\develop\POC\GUIWithPrism\Modules" };
}
}
Module:
namespace HelloWorldModule2
{
[Module(ModuleName="HelloWorldModule2")]
public class HelloWorldModule2 : IModule
{
private IApplicationMenuRegistry menuRegistry;
private HelloWorldModule2ViewModel viewModel;
private IRegionManager regionManager;
public HelloWorldModule2(IApplicationMenuRegistry menuRegistry, HelloWorldModule2ViewModel vm, IRegionManager regionManager)
{
this.menuRegistry = menuRegistry;
this.regionManager = regionManager;
this.viewModel = vm;
}
public void Initialize()
{
ObservableCollection<ViewObject> views = new ObservableCollection<ViewObject>();
views.Add(new ViewObject() { Region = RegionName.Right, ViewType = typeof(HelloWorld2View) });
views.Add(new ViewObject() { Region = RegionName.Left, ViewType = typeof(View2) });
//****** Here the module gets registered in the main menu ******//
menuRegistry.RegisterModuleMenuItem("HelloWorld2", "Hello World module 2",views,1);
this.viewModel.Title = "Hello world module 2";
}
}
}
View Model:
namespace HelloWorldModule2.ViewModels
{
public class HelloWorldModule2ViewModel : NotificationObject
{
private string title;
public string Title
{
get { return title; }
set
{
title = value;
RaisePropertyChanged(() => this.Title);
}
}
}
}
I ran into the following problem:
When i register the view model in the Unity container as seen in the Bootstrapper code, my module doesn't get initialized (I set a breakpoint in the Initialize
method of the module but it never
hits). If I remove the registration and remove the vm parameter in the constructor of the module, the module DOES initialize.
Also when i manual configure the module catalog with this module with:
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(HelloWorldModule2.HelloWorldModule2));
}
instead of the DirectoryModuleCatalog
, it initializes correctly even WITH the registration of the view model.