【发布时间】:2020-02-13 10:09:43
【问题描述】:
我需要在 ListView 中显示非常大的数据。我必须加载所有 数据一次?我的代码如下:
using System.Collections.ObjectModel;
namespace Brunie.Mobile.ViewModels {
public class VmListCompany {
public ObservableCollection<Company> AllCompanies {
get;
} = new ObservableCollection<Company>();
public VmListCompany() {
LoadAllCompanies();
}
private void LoadAllCompanies() {
Company company = null;
while(null != (company = GetNextCompany(company))) {
AllCompanies.Add(company);
}
}
private bool HasNextCompany(Company company) {
bool hasNextCompany = false;
......
return (hasNextCompany);
}
private Company GetNextCompany(Company company) {
if(!HasNextCompany(company)) {
return (null);
}
Company nextCompany = new Company();
......
return (nextCompany);
}
}
public class Company {
public string Name {
get;
set;
}
public string Address {
get;
set;
}
public int NumberOfEmployees {
get;
set;
}
}
}
Xaml 中的列表视图:
<ListView ItemsSource="{Binding AllCompanies}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Name}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
调用 LoadAllCompanies 后,AllCompanies 的计数为 20562104。 所以我的应用程序现在很慢。 这种情况如何处理?
【问题讨论】: