2013-04-10 60 views
1

在這裏工作是我的元數據:MEF ImportMany不Caliburn.Micro

[MetadataAttribute] 
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] 
public class ModuleAttribute : ExportAttribute, IModuleMetadata 
{ 
    public ModuleAttribute(string Contract) : base(Contract,typeof(IScreen)) 
    {    
     Region = Region.Sidebar; 
     IsVisible = true; 
     Order = short.MaxValue; 
     Description = string.Empty; 
    } 

    public string Module { get; set; }   
    public Region Region { get; set; }   
    public string DisplayName { get; set; }   
    public bool IsVisible { get; set; } 
    public string Description { get; set; } 
    public short Order { get; set; } 
} 

我的界面:

public interface IModuleMetadata 
{  
    string Module { get; set; } 
    Region Region { get; set; } 
    string DisplayName { get; set; } 
    bool IsVisible { get; set; } 
    string Description { get; set; } 
    short Order { get; set; } 
} 

我使用訪問:

[ImportMany] 
public IEnumerable<Lazy<IScreen, IModuleMetadata>> Mods 
{ 
    get; 
    set; 
} 

但始終我MODS的結束是null

更新:

[Module("Unit", Module = "Stock")] 
class UnitViewModel : BaseViewModel, ICUDB, IHandle<UnitModel> 
{ 

有趣的是,當我問使用GetExport。我得到所有15個導出的類。

var ex = container.GetExports<IScreen, JIMS.Common.Interface.IModuleMetadata>(); 
+0

向我們展示如何使用ModuleAttribute。問題可能在於此。 – 2013-04-11 09:38:51

+0

我已經更新了我的問題 – 2013-04-11 13:53:26

回答

0

您需要檢查以確保您匹配的合同意味着挖掘元數據。雖然我建議您這樣做,這樣您可以看到MEF如何將所有零件組合在一起,但您應該嘗試指定type for ImportMany

0

另一件要檢查的是如何創建BaseViewModel。如果您從shell或其他位置執行了新的BaseViewModel(),則MEF將無法在類中使用它的魔法。 MEF需要創建任何將使用MEF的對象。

+0

我的BaseViewModel是從實現IScreen的屏幕繼承的 – 2013-04-11 14:52:21

+0

這並不重要,這一切都取決於如何創建對象。 – 2013-04-11 17:42:57

0

由於您正在使用合同進行出口,因此您需要輸入相同的合同。

像:

[ImportMany("Unit")] 
public IEnumerable<Lazy<IScreen, IModuleMetadata>> Mods 

另一個方法,使這項工作是修改出口到:

[Module(null, Module = "Stock")] 
class UnitViewModel : BaseViewModel, ICUDB, IHandle<UnitModel> 

通過傳遞null或空字符串作爲合同那麼類型會用過的。

如果您並不總是需要合同,那麼您可以將一個無參數的.ctor添加到ModuleAttribute

+0

沒有這方面的幫助,我試過你說的,但仍然是空的 – 2013-04-11 16:49:43

+0

你沒有合同試過嗎?將它從導出(通過將其設置爲空)和導入(保持原樣)中刪除。如果仍然無法使用DisableSilentRejection選項創建組合容器並檢查拒絕原因。 – 2013-04-11 17:28:48