Caliburn Micro 发布/订阅

Caliburn Micro publish / subscribe

我正在学习以下教程 http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/ 我目前停留在发布/订阅部分。

我已设置好所有内容,因此它应该实际发布事件,但订阅视图模型没有收到消息。

我做了以下事情:

正在发布 ViewModel:

[Export(typeof(ColorViewModel))]
public class ColorViewModel : PropertyChangedBase
{
    private readonly IEventAggregator events;

    [ImportingConstructor]
    public ColorViewModel(IEventAggregator events)
    {
        this.events = events;
    }

    public void Red()
    {
        this.events.PublishOnUIThread(new ColorEvent(new SolidColorBrush(Colors.Red)));
    }

    public void Green()
    {
        this.events.PublishOnUIThread(new ColorEvent(new SolidColorBrush(Colors.Green)));
    }

    public void Blue()
    {
        this.events.PublishOnUIThread(new ColorEvent(new SolidColorBrush(Colors.Blue)));
    }
}

正在订阅 ViewModel:

[Export(typeof(AppViewModel))]
public class AppViewModel : PropertyChangedBase, IAppViewModel, IHandle<ColorEvent>
{
    private IEventAggregator events;

    [ImportingConstructor]
    public AppViewModel(ColorViewModel colorViewModel, IEventAggregator events)
    {
        this.ColorViewModel = colorViewModel;

        this.events = events;
        this.events.Subscribe(this);
    }

    public ColorViewModel ColorViewModel { get; private set; }

    private SolidColorBrush color;

    public SolidColorBrush Color
    {
        get
        {
            return this.color;
        }

        set
        {
            this.color = value;
            this.NotifyOfPropertyChange(() => this.Color);
        }
    }

    public void Handle(ColorEvent message)
    {
        this.Color = message.Color;
    }
}

ColorView 上有 3 个单选按钮,我可以单击它们,然后进入 Red()、Green()、Blue() 方法,以便调用 PublishOnUIThread。 但是我从来没有达到 AppViewModel 的 Handle(ColorEvent) 方法。

我是不是遗漏了什么或者为什么在发布 ColorEvents 后我的 handle 方法没有被调用?

提前致谢

事件聚合器来自哪里?它是 AppViewModelColorViewModel 之间共享的同一个实例吗?

确保事件聚合器在依赖注入器中注册为单例。