I'm working through the following tutorial http://www.mindscapehq.com/blog/index.php/2012/2/1/caliburn-micro-part-4-the-event-aggregator/ and I'm currently stuck at the publish / subscribe part.
I have everything set up, so that it should actually publish the events but the subscribing viewmodel doesn't get the message.
I've done the following:
Publishing 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)));
}
}
Subscribing 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;
}
}
There are 3 radio buttons on the ColorView which I can click and I do get into the Red(), Green(), Blue() methods so that the PublishOnUIThread is called. But I never reach the Handle(ColorEvent) method of the AppViewModel.
Am I missing something or why doesn't my handle method gets called after publishing the ColorEvents?
Thanks in advance
Where is the event aggregator coming from? Is it the same instance shared between AppViewModel
and ColorViewModel
?
Make sure the event aggregator is registered as a singleton in the dependency injector.