0
假設要加載大量數據,我希望UI在加載數據時能夠響應。目前唯一的工作代碼是不斷刷新不需要的用戶界面。如何在非UI線程中加載數據並獲取視圖的最終更新?EF LoadAsync後刷新視圖
private static object sync_lock = new object();
private void Load()
{
MyEntities db = new MyEntities();
TestEntityViewModel testEntityViewModel = (TestEntityViewModel)FindResource("testEntityViewModel");
testEntityViewModel.Source = db.TestEntities.Local; // Source is ObservableCollection<TestEntity>
BindingOperations.EnableCollectionSynchronization(testEntityViewModel.Source, sync_lock);
db.TestEntities.LoadAsync().ContinueWith(new Action<Task>(
(t) =>
{
this.Dispatcher.Invoke(new Action(() =>
{
View.MoveCurrentToFirst();
CommandManager.InvalidateRequerySuggested();
}));
}));
}
注:如果我刪除調用EnableCollectionSynchronization
,數據加載,但ICollectionView
及其SourceCollection
將只有1項。