我在 UI 中有一个与 List Control 绑定的ObservableCollection属性。ViewModelItemSource
为了在运行时更新列表控件中显示的项目数,我ObservableCollection在ViewModel. 我使用了两种不同类型的代码片段,我将在下面分享它们,但仍然需要大量时间才能在列表控件中反映更新的数据。
代码片段 1:
public void AddRange(IEnumerable<T> items)
{
foreach (var item in items)
{
this.Items.Add(item);
}
}
代码片段 2:
void BatchAddPeople(IEnumerable<Person> newPeople)
{
var currentPeople = _people;
// stop WPF from listening to the changes that we're about
// to perform
this.People = null;
// change
foreach (var person in newPeople)
{
currentPeople.Add(person);
}
// cause WPF to rebind--but only once instead of once for
// each person
this.People = currentPeople; //Updating the ObservableCollection property with complete list
}
我已经尝试了这两种方法,使用BackGroundWorker线程更新方法ObservableCollection内的代码Dispatcher.Invoke,但我仍然面临性能问题。
你们对如何减少这种情况下的性能损失有任何想法吗?