从DLL加载Prism模块

本文关键字:模块 Prism 加载 DLL | 更新日期: 2023-09-27 18:21:05

最近我对开发PRISM WPF应用程序产生了兴趣。现在,我正试图从构建模块项目(Wpf用户控制库)后创建的DLL中加载我的模块。在模块项目的构建过程中,我复制调试文件夹中的DLL(副本:xcopy/y"$(TargetPath)"$(SolutionDir)FooBar''$(OutDir)modules''")。接下来,我配置引导程序,我想有是我丢失了它。

我将在下面附上我的代码。

引导程序

 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。有人能帮我吗?

从DLL加载Prism模块

您的模块目录为空吗?或者不包含模块?检查Environment.CurrentDirectory+@"''Modules";返回正确的路径,并且所有DLL都在中。你真的需要从目录加载它们吗?你不知道会加载哪些模块吗?

我通常这样做:

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

和我的模块目录

<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)"