2013-07-04 140 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

生病嘗試你的解決方案感謝您的輸入 –