I have WPF application implemented with classic MVVM pattern, however View, Model and ViewModel are three different projects in my solution.
I have well-known implementation of AsyncObservebleCollection in my ViewModel
public class MyVM : ViewModelBase
{
private RelayCommand _runCommand;
private AsyncObservebleCollection _messages;
public AsyncObservebleCollection<Message> Messages
{
get
{
return _messages;
}
set
{
_messages = value; NotifyPropertyChanged();
}
}
public ICommand RunCommand
{
get
{
if (_runCommand == null)
{
_runCommand = new RelayCommand(executeParam =>
bq.QueueTask(this.ExecuteCommand),
canExecuteParam => true);
}
return _runCommand;
}
}
}
bq.QueueTask(this.ExecuteCommand) -> backgroundWorker is executing command on the background thread and it updates Messages property which is bidden to my view.
Now, I am getting the thread violation exception because AsyncObservebleCollection doesn't have UI's SynchronizationContext, and since I do know that ViewModel should not know about the View, how do I solve my problem? How do I use my UI's SynchronizationContext to run my RelayCommand asynchronously and update the UI?
Thank you