Luis Guzman
Luis Guzman

Reputation: 65

Loading data incrementally Collectionview MAUI with RemainingItemsCommand

I have a Page with a collectionview and is working fine, but currently, I am trying to implement RemainingItemsThresholdReachedCommand with no success. The problem is that the command is continously firing even without scrolling the collection and also, the scrolling of the collection gets very laggy when the command is enable.

The collection is inside a Grid and the Grid is inside a Scrollview, like this:

<Scrollview>
    <Grid>
        <CollectionView>
        </CollectionView>
    </Grid>
</Scrollview>

This is my code.

XAML

<CollectionView Grid.Row="1" 
                Grid.Column="0" 
                Grid.ColumnSpan="2" 
                ItemsSource="{Binding Messages}" 
                SelectionMode="None" 
                BackgroundColor="{AppThemeBinding Dark=Black,Light=White}" 
                RemainingItemsThreshold="0" 
                RemainingItemsThresholdReachedCommand="{Binding RemainingItemsCommand}">
                
            <CollectionView.ItemTemplate>
                 <DataTemplate x:DataType="models:MessagesMessages">

                 </DataTemplate>
            </CollectionView.ItemTemplate>
</CollectionView>

ViewModel

public partial class MessagesPageViewModel : ObservableObject
{
    public ObservableCollection<MessagesMessages> Messages { get; set; } = new();

    [RelayCommand]
    private void RemainingItems()
    {
        LoadMessages();
    }
    
    public MessagesPageViewModel(IApiService apiService)
    {
        apiService = apiService;
        LoadMessages();
    }

    private async void LoadMessages()
    {
        Messages.Add(...)
    }
}

I have tried to change the RemainingItemsThreshold value to every number and it does not work as expected. I am retrieving data from an API in packages of 10 messages, and the first load fill the entire screen, but without scrolling, the command continue to fire.

enter image description here

Upvotes: 2

Views: 2659

Answers (1)

Liqun Shen-MSFT
Liqun Shen-MSFT

Reputation: 7990

It's not recommended to wrap a vertically-scrolling CollectionView inside a vertically-scrolling ScrollView. CollectionViews have their own scrolling built in so you don't have to put it in a ScrollView. Otherwise, it will cause many scrolling issues. If you still want to put it in a scrollview, try to set the HeightRequest of the CollectionView.

For more info, you could refer to: CollectionView requires ScrollView #8097, How to use CollectionView inside a ScrollView.

Upvotes: 2

Related Questions