C#EventPublisher的实现

时间:2011-03-06 17:01:00

标签: c# wpf events mvvm event-handling

我知道Microsoft Prism有一个EventAggregator。我还在CodeProject上找到了one

还有更好的选择吗?

由于

更新

刚刚发现了一个非常相似的问题(2010年2月发布):https://stackoverflow.com/questions/2343980/event-aggregator-implementation-sample-best-practices

3 个答案:

答案 0 :(得分:1)

对于WPF / Silverlight。我已经使用了Messenger Class中的MVVM Light toolkit。我认为它也可以在Windows窗体中使用。

你这样使用它(来自上面的链接):

// Somewhere
Messenger.Default.Register<string>(this, DoSomething);
// Further
private void DoSomething(string message)
{
    // ...
}
// Somewhere else
Messenger.Default.Send(“Hello world”)

有关MVVM Light的更多信息,请参阅此处: http://www.galasoft.ch/mvvm/getstarted/

答案 1 :(得分:0)

我不知道它是否更好,但我已经实现并维护MemBus,我更喜欢描述更多的“内存发布/订阅总线”,但实际上它很容易使用它作为事件聚合器。

发布,订阅非常易插拔,IoC支持易于添加等等。如果您使用Rx框架,它需要.NET 4并带来很多乐趣,因为MemBus可以创建IObservable实现。你会发现一些documentation here

答案 2 :(得分:0)

一般来说,这是一个nice implementation以及一个关于事件聚合的good and related write up

作为'善良'的试金石,这是Caliburn框架中使用的灵感。

HTH
Berryl