2013-03-20 122 views
0

目前,我有我的视图模型MVVM,MEF,Silverlight和服务代理

public CartViewModel() : this(new PayPalCompleted()) { } 

    public CartViewModel(IPayPalCompleted serviceAgent) 
    { 
     if (!IsDesignTime) 
     { 
      _ServiceAgent = serviceAgent; 
      WireCommands(); 
     } 
    } 

下面的构造,我想我的modularise应用棱镜与MEF。我的模块工作正常,但我遇到了我的一个视图模型的问题。

我的问题是,我需要在构造函数中导入EventAggregator,但我有我是如何做到这一点有paramaterless构造,以及作为一个进口构造

[ImportingConstructor] 
    public CartViewModel([Import] IEventAggregator eventAggregator) 
    { 
     if (!IsDesignTime) 
     { 
      _ServiceAgent = new PayPalCompleted(); 
      TheEventAggregator = eventAggregator; 
      WireCommands(); 

     } 
    } 

即我想要做的问题像这样的东西

 public CartViewModel() : this(new PayPalCompleted(), IEventAggregator eventAggregator) { } 

    [ImportingConstructor] 
    public CartViewModel(IPayPalCompleted serviceAgent, IEventAggregator eventAggregator) 
    { 
      ...stuff 
    } 

这是不正确的我知道...什么是?

问题的一部分,我认为是,当使用导入构造函数时,默认情况下构造函数中的参数是导入参数 - 这意味着它们需要MEF的相应导出才能正确编写。这可能意味着我应该导出我的paypay服务?还是应该?

感谢

回答

0

处理这个最简单的方法是揭露类型IEventAggregator的属性,实现IPartImportsSatisifiedNotification和该方法处理事件订阅。

像这样的事情

public class CartViewModel : IPartImportsSatisfiedNotification 
{ 
    private readonly IPayPalCompleted _serviceAgent; 

    public CartViewModel(IPayPalCompleted serviceAgent) 
    { 
     this._serviceAgent = serviceAgent; 
     CompositionInitializer.SatisfyImports(this); 
    } 

    [Import] 
    public IEventAggregator EventAggregator { get; set; } 

    void IPartImportsSatisfiedNotification.OnImportsSatisifed() 
    { 
     if (EventAggregator != null) 
     { 
      // Subscribe to events etc. 
     } 
    } 
} 
+0

我已经做了类似的事情,但忘了添加CompositionInitializer.SatisfyImports(本); !谢谢 – 72GM 2013-03-25 12:05:41