Assuming large set of data to be loaded, I want the UI be responsive while loading data. Currently the only working code is the following which constantly refreshes the UI which is not desired. How to have data loaded in non-UI thread and gets the final update on view?
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();
}));
}));
}
Note: If I remove the call to EnableCollectionSynchronization
, data is loaded but the ICollectionView
and its SourceCollection
would have only 1 item.