MVVM、MEF、Silverlight和服务代理

MVVM、MEF、Silverlight和服务代理,silverlight,mvvm,mef,Silverlight,Mvvm,Mef,我当前在viewmodel中有以下构造函数 public CartViewModel() : this(new PayPalCompleted()) { } public CartViewModel(IPayPalCompleted serviceAgent) { if (!IsDesignTime) { _ServiceAgent = serviceAgent; WireCommands(

我当前在viewmodel中有以下构造函数

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

    public CartViewModel(IPayPalCompleted serviceAgent)
    {
        if (!IsDesignTime)
        {
            _ServiceAgent = serviceAgent;
            WireCommands();
        }
    }
我正在尝试模块化我的应用程序Prism和MEF。我的模块工作正常,但我的一个ViewModel有问题

我的问题是我需要在构造函数中导入EventAggregator,但是我在如何使用无参数构造函数以及导入构造函数时遇到了问题

    [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
     }
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.
        }
    }
}
这是不对的我知道。。。什么是

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


感谢

处理此问题的最简单方法是公开IEventAggregator类型的属性,实现iPartimportSSatifiedNotification,并用该方法处理事件订阅

像这样的

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

    [ImportingConstructor]
    public CartViewModel(IPayPalCompleted serviceAgent, IEventAggregator eventAggregator)
    {
             ...stuff
     }
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.
        }
    }
}

我已经做了类似的事情,但忘记添加CompositionInitializer.satisfyiImports(这个)!!谢谢