従来の MVVM パターンで実装された WPF アプリケーションがありますが、View、Model、ViewModel は私のソリューションでは 3 つの異なるプロジェクトです。
ViewModelにAsyncObservebleCollectionのよく知られた実装があります
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 はバックグラウンド スレッドでコマンドを実行しており、ビューに入札されている Messages プロパティを更新します。
現在、AsyncObservebleCollection には UI の SynchronizationContext がないため、スレッド違反の例外が発生しています。また、ViewModel が View について認識してはならないことがわかっているため、問題を解決するにはどうすればよいですか? UI の SynchronizationContext を使用して RelayCommand を非同期に実行し、UI を更新するにはどうすればよいですか?
ありがとうございました