我正在完成以下教程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后被调用?
提前致谢
答案 0 :(得分:2)
事件聚合器来自哪里?它是AppViewModel
和ColorViewModel
之间共享的同一实例吗?
事件聚合器是注册为单例还是Ninject中的等价物?