I have an Api with pagination in which I have 1000 records but I want to show the first 20 and so on, on my CollectionView, I have seen an SO answer which sais that I should "Use Pagination in API side and load data on ListItemAppearing" but I don't know how to achieve this.
Here's the answer that I found: https://stackoverflow.com/a/60205866/18972374
Please help and thanks.
EDIT
I have seen that when having a listview it is possible to use Xamarin.Forms.Extended.InfiniteScrolling but it doesn't work for collectionView. Still don't know how to achieve this
EDIT 2
I'm trying to use this
RemainingItemsThreshold="4"
RemainingItemsThresholdReachedCommand="{Binding LoadMoreCommand}"
This is what I have but it keeps giving me this exception
System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
This is my LoadMore
method that I call on my LoadMoreCommand
private async void LoadMore()
{
if (IsBusy)
return;
IsBusy = true;
// load the next page
var page = ListCart.Count / pageSize;
//calling api
var carts = await GetListCart(pageSize, page);
_oListCart.AddRange(carts);
IsBusy = false;
}
This my CollectionView
<CollectionView x:Name="ListViewCart"
ItemsSource="{Binding ListCart}"
RemainingItemsThreshold="4"
RemainingItemsThresholdReachedCommand="{Binding LoadMoreCommand}"
VerticalScrollBarVisibility="Always"
SelectionMode = "Single"
SelectedItem="{Binding CartSelected}"
SelectionChangedCommand="{Binding ShowAlertCommand}"
SelectionChangedCommandParameter="{Binding CartSelected, Source={RelativeSource Self}}"
>....
CodePudding user response:
please refer to this official link Load data incrementally
CodePudding user response:
That InvalidOperationException
happens because Xamarin was using the ObservableCollection internally (to display items while scroll), at the time RemainingItemsThresholdReachedCommand
calls your command.
To fix this, slightly delay the line that adds to the collection. You do this by queueing it back to MainThread, to run after your command returns.
Change:
_oListCart.AddRange(carts);
IsBusy = false;
To:
Device.BeginInvokeOnMainThread(() =>
{
try
{
_oListCart.AddRange(carts);
}
finally
{
IsBusy = false;
}
});