C# MEF ImportMany不使用Caliburn.Micro

C# MEF ImportMany不使用Caliburn.Micro,c#,.net,wpf,mef,caliburn.micro,C#,.net,Wpf,Mef,Caliburn.micro,以下是我的元数据: [MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class ModuleAttribute : ExportAttribute, IModuleMetadata { public ModuleAttribute(string Contract) : base(Contract,typeof(IScreen)) {

以下是我的元数据:

[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;
}
有趣的是,当我要求使用GetExport时。我得到了所有15个导出类

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

您需要检查以确保匹配合同,这意味着深入挖掘元数据。我建议您这样做,这样您就可以看到MEF是如何将所有零件放在一起的,您应该尝试指定。

另一件要检查的事情是如何创建BaseViewModel。如果您从shell或其他位置创建了一个新的BaseViewModel(),MEF将无法在类中发挥其魔力。MEF需要创建任何将使用MEF的对象。

因为您将合同用于导出,所以您将需要相同的合同用于导入

比如:

[进口数量(“单位”)]
公共可数Mods
另一种执行此操作的方法是将导出修改为:

[Module(null, Module = "Stock")]
class UnitViewModel : BaseViewModel, ICUDB, IHandle<UnitModel>
[Module(null,Module=“Stock”)]
UnitViewModel类:BaseViewModel、ICUDB、IHandle
通过将null或空字符串作为协定传递,将使用该类型


如果您并不总是需要合同,那么您可以将无参数.ctor添加到
ModuleAttribute

中,向我们展示如何使用ModuleAttribute。问题可能就在那里。我已经更新了我的问题。我的BaseViewModel是从实现IsScreen的屏幕继承的。这没关系,这完全取决于对象是如何创建的。没有帮助,我尝试了你说的,但仍然是空的。你尝试过没有合同吗?将其从导出(通过将其设置为null)和导入(保持原样)中删除。如果仍然不起作用,请在启用DisablesInRejection选项的情况下创建合成容器,并检查拒绝原因。
[ImportMany("Unit")]
public IEnumerable<Lazy<IScreen, IModuleMetadata>> Mods
[Module(null, Module = "Stock")]
class UnitViewModel : BaseViewModel, ICUDB, IHandle<UnitModel>