In my view I have a Grid that is bound to an ObservableCollection. My ViewModel contains the ObservableCollection
public ObservableCollection<Session> Sessions
{
get
{
return _repository.GetSessions();
}
set
{
_sessions = value;
NotifyPropertyChanged("Sessions");
}
}
When some action happens in another ViewModel, I publish an event to the event aggregator, I'm subscribing to that event in the ViewModel that contains the Sessions property and modifying an object in the Sessions ObjectCollection.
When I publish an event to the event aggregator from the UI thread, the subscriber's event handler does the work and the UI is updated as expected.
The problem is when I publish an event to the event aggregator from a background worker. The code in the subscriber is executed, but the UI is not updated.
Here's some code:
SecondaryViewModel.cs publishing events from the UI thread.
_eventAggregator.GetEvent<SomethingChangedEvent>().Publish("this changed");
In MainViewModel.cs I'm subscribing to the event and updating a property and the UI DOES update.
//The subscription
_eventAggregator.GetEvent<SomethingChangedEvent>().Subscribe(SomethingChangedEventHandler, ThreadOption.UIThread);
Background Worker
//Check stuff in background
private void CheckSessionsInBackground()
{
//Show your wait dialog
var worker = new BackgroundWorker();
worker.DoWork += DoWork;
worker.RunWorkerCompleted += WorkerCompleted;
worker.RunWorkerAsync();
}
And in the DoWork Method I'm publishing an event
private void DoWork(object sender, DoWorkEventArgs e)
{
//Some other work
_eventAggregator.GetEvent<SomethingChangedEvent>().Publish("Changes from the background worker");
}
If I stop execution in the handler I can confirm that the code is being executed, however the UI is not updated. My question is if it's possible to update a collection when a message is published to the eventaggregator in a background worker. I've looked at examples that use the Dispatcher, but none of them use the EventAggregator. Any help is welcome.