2010-06-03 200 views
2

我正在學習MEF,但無法解決問題。我有一個名爲MainMEF的主應用程序,和一個名爲SimpleModule的簡單模塊。這一個由一個動態加載的UserControl組成。Mef,將參數傳遞給模塊

當MainMEF啓動時,我將能夠傳遞給模塊MainMEF中包含的主應用程序的引用。

我該如何解決這個問題?

回答

6

很多有關這方面的問題。 你可以初始化後使用屬性傳遞: How do I populate a MEF plugin with data that is not hard coded into the assembly?

或者使用MEF構造函數的參數: MEF Constructor Parameters with Multiple Constructors

出口看起來是這樣的:撰寫你的目錄做時

[Export(typeof(ITest))] 
class Test : ITest 
{ 
    void Test() 
    { } 

    [ImportingConstructor] //<- This is the key bit here 
    void Test(object parameter) 
    { } 
} 

然後:

catalog.ComposeExportedValue(/* parameter here */); 
catalog.ComposeParts(this); 
+0

謝謝您的回覆。很有幫助。你能用更多的代碼向我解釋這些行嗎?考慮目錄。 ### catalog.ComposeExportedValue(/ * parameter here * /); catalog.ComposeParts(this); ### 它屬於哪種類型? – 2010-06-04 08:10:40