Async OOPで定義されたパターンに従って、2つのソースからデータを読み込むビューモデルを作成しました。しかし、私はコレクションのためにセットアップCollectionViewSource
とフィルタにしようとしたとき、私は例外Additional information: Must create DependencySource on same Thread as the DependencyObject.
を取得したり、データをフィルタするとき、私は非同期ViewModelコンストラクタのデータの初期化
public NotifyTaskCompletion InitializationNotifier{ get; set; }
public Task Initialization => InitializationNotifier.Task;
のviewmodelコンストラクタの定義を以下のようにThe calling thread cannot access this object because a different thread owns it.
プロパティが定義されている以下の例外を取得しますInitializationNotifier
は次のように
InitializationNotifier = new NotifyTaskCompletion(InitializeAsync());
タスクは以下のように定義されます
private async Task InitializeAsync()
{
var aDataSource = await Task.Run(() => ADataSource.Get(1, 2));
var bDataSource = await Task.Run(() => new BDataSource(1, 2);
PrepareData(aDataSource, bDataSource); // creates List<T> for different categories
SetupCollections(); // Creates Observable collections from List<T>
SetupCVSAndFilters(); // Creates CollectionViewSource for different categories
}
private void SetupCollections()
{
AStars = new ObservableCollection<IAEntity>(m_aStars);
BStars = new ObservableCollection<IAEntity>(m_bStars);
}
// Setup Collectionview source and Filters
// Get an exception:
// Additional information: Must create DependencySource on same Thread as the DependencyObject.
private void SetupCVSAndFilters()
{
AStarsCVS = new CollectionViewSource { Source = AStars };
BStarsCVS = new CollectionViewSource { Source = BStars };
AStarsCVS.View.Filter = FilterCompareData;
BStarsCVS.View.Filter = FilterCompareData;
}
ビュー/のviewmodelはAsync
/Task
を使用せずにうまく動作しますが、データソースからのフェッチは、時間のかかる(PInvoke
S)であり、私は別のデータを読み取る複数のタブを持っていると私は私が理解するためのポインタを感謝のですここで私は使用方法が間違っていますTask
/Async
ここで何が起きているのか本当に分かっていれば、同時実行ビジュアライザ(https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ConcurrencyVisualizer2017)をインストールすることができます。単純なAPI(https://www.nuget.org/packages/ConcurrencyVisualizer/)で使用すると、コード内のセクションにマークを付け、実行されているスレッドを確認できます(https://msdn.microsoft .com/en-us/en-en/library/hh755853.aspx)。それは本当にクールです。 –