从DLL加载Prism模块

时间:2013-07-04 11:55:48

标签: c# wpf module bootstrapping prism-4

最近我对开发PRISM WPF应用程序感兴趣。现在我正在尝试从构建模块项目(Wpf用户控件库)之后的DLL中加载我的模块。在构建模块项目期间,我将DLL复制到调试文件夹中(copy:xcopy / y“$(TargetPath)”“$(SolutionDir)FooBar \ $(OutDir)Modules \”)。接下来我配置了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()
    {

    }
}

所以我想知道为什么我的ModuleCatalog.Modules总是0.有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您的模块目录是否为空?或者不包含模块? 检查Environment.CurrentDirectory + @“\ Modules”;返回正确的路径,所有DLL都在。 你真的需要从dir加载它们吗?你不知道将加载哪些模块吗?

我通常会这样做:

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;
    }
}

和我的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>

并且所有模块都有buildAction:copy“$(TargetPath)”“$(SolutionDir)Shell \ $(OutDir)”