0

最近,我对开发 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();
    }

应用程序.xaml.cs

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

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

视图模型库

    public abstract class ViewModelBase : INotifyPropertyChanging, INotifyPropertyChanged,IModule
    {

      //Implementation INotify etc..

    public void Initialize()
    {

    }
}

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

4

1 回答 1

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

和我的模块目录

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

于 2013-07-04T15:18:47.000 回答