Search code examples
c#wpfmodulebootstrappingprism-4

Load Prism modules from DLL


Recently I took an interest in developing PRISM WPF applications. Now I'm trying to load my modules from a DLL I make after building the Modules Project (Wpf User Control Library). During the build of the modules project I copy the DLL in the debug folder (copy: xcopy /y "$(TargetPath)" "$(SolutionDir)FooBar\$(OutDir)Modules\"). Next I configure the bootstrapper and I think there is were I lost it.

I'll attach my code below.

Bootstrapper

 public class Bootstrapper : UnityBootstrapper
 {
    protected override DependencyObject CreateShell()
    {
        var shell = ServiceLocator.Current.GetInstance<Shell>();
        return shell;
    }

    protected override void InitializeShell()
    {
        base.InitializeShell();

        App.Current.MainWindow = (Window)this.Shell;
        App.Current.MainWindow.Show();
    }

    protected override IModuleCatalog CreateModuleCatalog()
    {
        return base.CreateModuleCatalog();
    }

    protected override void ConfigureModuleCatalog()
    {
        var moduleCatalog = new DirectoryModuleCatalog();
        moduleCatalog.ModulePath = Environment.CurrentDirectory + @"\Modules";
        ModuleCatalog = moduleCatalog;
    }

    protected override void InitializeModules()
    {
        base.InitializeModules();
    }

    protected override ILoggerFacade CreateLogger()
    {
        return base.CreateLogger();
    }
}

Shell.xaml.cs

    protected readonly IModuleCatalog _moduleCatalog;

    public Shell(IModuleCatalog moduleCatalog)
    {
        this._moduleCatalog = moduleCatalog;
        InitializeComponent();
    }

App.xaml.cs

    public partial class App : Application
    {
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        var bootstrapper = new Bootstrapper();
        bootstrapper.Run();
    }

ViewModelBase

    public abstract class ViewModelBase : INotifyPropertyChanging, INotifyPropertyChanged,IModule
    {

      //Implementation INotify etc..

    public void Initialize()
    {

    }
}

So I'm wondering why my ModuleCatalog.Modules is always 0. Can someone help me out ?


Solution

  • Is your module catalog null? or contains no module? check that Environment.CurrentDirectory + @"\Modules"; returns the correct path and all DLL are in. Do you really need to load them from dir? Don't you know which modules will be loaded?

    I usualy do that:

    public partial class Bootstrapper: UnityBootstrapper
    {
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
        }
    
        protected override IModuleCatalog CreateModuleCatalog()
        {
            // Create the module catalog from a XAML file.
            return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("/Shell;component/ModuleCatalog.xaml", UriKind.Relative));
        }
    
        protected override DependencyObject CreateShell()
        {
            // Use the container to create an instance of the shell.
            ShellView view = Container.TryResolve<ShellView>();
    
            // Display the shell's root visual.
            //view.ShowActivated = false;
            view.Show();
    
            return view;
        }
    }
    

    and my modulecatalog

    <prism:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:prism="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
    
    
    
    
    <prism:ModuleInfo Ref="Connexion.dll"
                      ModuleName="Connexion"
                      ModuleType="Connexion.ModuleInit, Connexion, Version=1.0.0.0" />
    
    <prism:ModuleInfo Ref="Tools.dll"
                      ModuleName="Tools"
                      ModuleType="Tools.ModuleInit, Tools, Version=1.0.0.0" />
    
    
    <prism:ModuleInfo Ref="DrawingModule.dll"
                      ModuleName="DrawingModule"
                      ModuleType="DrawingModule.ModuleInit, DrawingModule, Version=1.0.0.0"
                      InitializationMode="WhenAvailable">
        <prism:ModuleInfo.DependsOn>
            <sys:String>Connexion</sys:String>
            <sys:String>Tools</sys:String>
        </prism:ModuleInfo.DependsOn>
    </prism:ModuleInfo>
    
    
    
    <prism:ModuleInfo Ref="Sis.dll"
                      ModuleName="Sis"
                      ModuleType="Sis.ModuleInit, Sis, Version=1.0.0.0"
                      InitializationMode="WhenAvailable">
        <prism:ModuleInfo.DependsOn>
            <sys:String>Connexion</sys:String>
            <sys:String>Tools</sys:String>
            <sys:String>DrawingModule</sys:String>
        </prism:ModuleInfo.DependsOn>
    </prism:ModuleInfo>
    

    and all modules have the buildAction: copy "$(TargetPath)" "$(SolutionDir)Shell\$(OutDir)"