我正在完成以下教程 http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/ 我目前停留在发布/订阅部分。
我已完成所有设置,因此它实际上应该发布事件,但订阅 View 模型没有收到消息。
我已完成以下操作:
发布 View 模型:
[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 后我的处理方法没有被调用?
提前致谢
最佳答案
事件聚合器来自哪里?它是在 AppViewModel
和 ColorViewModel
之间共享的同一个实例吗?
确保事件聚合器在依赖注入(inject)器中注册为单例。
关于c# - Caliburn Micro 发布/订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29079745/