导入allways返回null,但ImportConstructor的工作

问题描述:

我想在我的应用程序中使用MEF,但我有导入的问题。导入allways返回null,但ImportConstructor的工作

[Import (typeof(IUserServices))] 
    public IUserServices UserService { get; private set; } 

这不起作用,UserService始终为空。

但是使用在同一类的ImportContstructor完美的作品:

[ImportingConstructor ] 
    public MainWindowVM(
     IUIVisualizerService uiVisualizer, 
     IViewAwareStatus viewAwareStatus, 
     IMessageBoxService messageBoxService, 
     IManager mwManager, 
     TagItemModel tagModel, 
     ILibraryModel documentModel, 
     ILibraryServices libraryServices, 
     ILogServices logServices , 
     IUserServices userServices) 

任何人可以帮助我的问题。我已经花了数小时,但没有找到任何解决方案。谢谢!!!

+0

您可以添加演示您的容器创建的代码,以及如何尝试满足导入吗? –

+0

我使用ChinchV2和MefedMVVM一起创建容器。 这里的代码,其中提供了出口: [PartCreationPolicy(CreationPolicy.Shared)] [导出(typeof运算(IUserServices))] 公共类TestUserServices:IUserServices { 公共无效的getSettings(动作回调) var dPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly()。GetName()。CodeBase).Remove(0,6); callback(新的HubSettings {DataPath = dPath},null); } } – MrSkyNavi

+0

我最好的猜测是这是MefedMVVM的一个问题。你确定它应该支持财产进口吗? –

我使用ChinchV2和MefedMVVM一起创建容器。 这里的代码,它提供了出口:

[PartCreationPolicy(CreationPolicy.Shared)] 
[Export (typeof(IUserServices))] 
public class TestUserServices:IUserServices 
{ 
    public void GetSettings(Action<HubSettings, Exception> callback) 
    { 
     var dPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6); 
     callback(new HubSettings {DataPath = dPath}, null); 
    } 
} 
+0

CTRL + K或点击编辑器格式代码上方的“{}”图标!欢迎来到SO! –

酒店将只能通过MEF构造后设置完全执行。你什么时候检查该属性是否为空?

+0

就是这样。我在初始化期间总是检查。非常感谢! – MrSkyNavi