In ViewModel
public async System.Threading.Tasks.Task<ObservableCollection<Data>> RefreshDataAsync()
{
Collection = new ObservableCollection<Data>();
RestUrl = "Api service link";
var uri = new Uri(RestUrl);
try
{
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Items = JsonConvert.DeserializeObject<FullData>(content);
for (int i = 0; i < 5; i++)
{
var data = new Data()
{
_Name = Items [i].Name,
_Id=Items[i].Id
};
Collection.Add(data);
}
return Collection;
}
}
}
In Behavior.cs
dataPager.OnDemandLoading += DataPager_OnDemandLoading;
private void DataPager_OnDemandLoading(object sender, OnDemandLoadingEventArgs e)
{
Device.BeginInvokeOnMainThread(async () => {
await Task.Delay(100);
var source = viewModel.ItemsSource.Skip(e.StartIndex).Take(e.PageSize);
ListView.ItemsSource = source.ToList<Data>();
dataPager.LoadDynamicItems(e.StartIndex, source);
dataPager.PagedSource.UseOnDemandPaging = true;
});
} |