Forms Xamarin表单:如何在ListView中处理大数据?

Forms Xamarin表单:如何在ListView中处理大数据?,forms,listview,xamarin,Forms,Listview,Xamarin,我需要在列表视图中显示非常大的数据。我必须把所有的东西都装进去吗 数据一次?我的代码如下: using System.Collections.ObjectModel; namespace Brunie.Mobile.ViewModels { public class VmListCompany { public ObservableCollection<Company> AllCompanies { get; } =

我需要在列表视图中显示非常大的数据。我必须把所有的东西都装进去吗 数据一次?我的代码如下:

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;
        }
    }
}
使用System.Collections.ObjectModel;
命名空间Brunie.Mobile.ViewModels{
上市公司{
公开收集所有公司{
得到;
}=新的ObservableCollection();
上市公司(){
加载所有公司();
}
私人有限公司(){
公司=空;
while(null!=(company=GetNextCompany(company))){
所有公司。添加(公司);
}
}
私人bool HASSNEXTCOMPANY(公司){
bool hastnextcompany=false;
......
返回(hasNextCompany);
}
私人公司GetNextCompany(公司){
如果(!HasNextCompany(公司)){
返回(空);
}
公司下一家公司=新公司();
......
返回(nextCompany);
}
}
公营公司{
公共字符串名{
得到;
设置
}
公共字符串地址{
得到;
设置
}
公共雇员人数{
得到;
设置
}
}
}
Xaml中的ListView:


调用LoadAllCompanys后,所有公司的计数为20562104。 所以我的应用程序现在很慢。 如何处理这种情况


在API端使用分页,并在ListItem上加载数据,正如@Chetan所说的那样,使用API分页是最好的方法。 也就是说,如果出于某种原因您不能更改分页(比如说它是第三方API),您仍然可以通过优化listview的缓存策略来提高性能
如图所示,这是否回答了您的问题?Hi@King23,但在正常情况下,用户不检查20562104记录,所以我们需要在ListView中加载数据。我认为我们需要在列表中加载数据,并在UI中使用分页side@ChetanRawat别误会我。您的答案是正确的,分页是一种方式,但鉴于他有很多记录,如果用户不断滚动分页页面,建议优化列表视图以进一步增强用户体验
<ListView ItemsSource="{Binding AllCompanies}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding Name}" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>