2013-07-04 97 views
0

最近我对开发PRISM WPF应用程序感兴趣。现在我试图从构建模块项目(Wpf用户控制库)后生成的DLL中加载我的模块。在构建模块项目期间,我将DLL复制到调试文件夹(复制:xcopy/y“$(TargetPath)”“$(SolutionDir)FooBar \ $(OutDir)Modules \”)。接下来,我配置引导程序,我认为有我失去了它。从DLL中加载棱镜模块

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

引导程序

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.有人可以帮我吗?

+0

我使用像您一样的目录技术,但不指定目录。 *但是*模块必须与包含引导记录器的程序集位于同一目录中!这是我很高兴与之共存的一个限制。 –

+0

我不得不添加moduleCatalog.Initialize();到用于棱镜的ConfigureModuleCatalog查找任何模块。 – Rtype

回答

1

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

我usualy做到这一点:

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的:复制 “$(TARGETPATH)”“$(SolutionDir)外壳\ $(OutDir)“

+0

当你拥有同一个DLL中的所有模块时,你能达到相同的结果吗? –

+0

我不认为这是可能的,除非你有多个程序集名称在DLL中,与多个模块初始化...但似乎并不干净,也不好... – Enhakiel

+0

生病尝试你的解决方案感谢您的输入 –