2012-09-20 87 views
13

我只是在學習WPF和Caliburn.Micro。我正在按照這裏提供的代碼: http://caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%20Bootstrapper&referringTitle=DocumentationCaliburn.Micro和MEF上wpf

顯然這個代碼是爲Silverlight,但我的項目是一個WPF,因此我收到錯誤,CompositionHost沒有定義。

該文件指出,我需要直接在wpf中初始化容器,但沒有文檔顯示如何。我怎樣才能直接初始化容器?

編輯1 引導捆紮機是這樣的文件:

 container = CompositionHost.Initialize(
     new AggregateCatalog(
      AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>() 
      ) 
     ); 

    var batch = new CompositionBatch(); 

    batch.AddExportedValue<IWindowManager>(new WindowManager()); 
    batch.AddExportedValue<IEventAggregator>(new EventAggregator()); 
    batch.AddExportedValue(container); 

    container.Compose(batch); 

,我又把它轉換爲:

var catalog = 
      new AggregateCatalog(
       AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()); 

     this.container = new CompositionContainer(catalog); 
     var batch = new CompositionBatch(); 

     batch.AddExportedValue<IWindowManager>(new WindowManager()); 
     batch.AddExportedValue<IEventAggregator>(new EventAggregator()); 
     batch.AddExportedValue(this.container); 

     this.container.Compose(batch); 

但是當我運行應用程序時,我收到錯誤MEF不能找到實施IShell

 Could not locate any instances of contract IShell. 

我相信我的初始化M EF不正確。你能幫我解決嗎?

回答

17

在WPF中,您需要使用明確的CompositionContainer構造函數。在我的WPF和Silverlight共享引導程序我已經使用了以下#if - #else指令:

#if SILVERLIGHT 
    container = CompositionHost.Initialize(catalog); 
#else 
    container = new CompositionContainer(catalog); ; 
#endif 

編輯

引導程序將確定一個實現IShell接口的組件(前提是您的引導程序擴展Bootstrapper<IShell>基類),所以你需要實現一個裝飾有MEF輸出的類IShell

通常,這將是你ShellViewModel和聲明是這樣的:

[Export(typeof(IShell))] 
public class ShellViewModel : PropertyChangedBase, IShell 
{ 
    ... 
} 

你可以閱讀更多關於引導程序設置和定製here

+0

謝謝。請參閱我的編輯問題。 – mans

+0

@mans請參閱我的答案更新。 –