如何在ViewModel中初始化属性从IEventAggregator中的事件?

时间:2012-03-22 14:32:10

标签: c# wpf prism

如果我有Microsoft.Practices.Prism.Events.IEventAggregator实例:

eventAggregator.GetEvent<MyEvent>.Subscribe(SomeMethod);

我在ViewModel中有一个属性,我想使用MyEvent类中的成员进行初始化,我该怎么做?

1 个答案:

答案 0 :(得分:0)

Prism不会将MyEvent类的属性传递给SomeMethod。它通过payloads作为DTO:

public class MyEvent: CompositePresentationEvent<MyEventArgs> {}

public class MyEventArgs { int MyIntValue; }

public class Subscriber
{
  public Subscriber
  {
    eventAggregator.GetEvent<MyEvent>().Subscribe(SomeMethod);
  }

  public void SomeMethod(MyEventArgs e)
  {
    MessageBox.Show(e.MyIntValue);
  }
}

public class Publisher
{
  public void SendMinusOne()
  {
    var args = new MyEventArgs() { MyIntValue = -1 };
    eventAggregator.GetEvent<MyEvent>().Publish(args);
  }
}