CollectionView RemainingItemsThresholdReached 事件问题

问题描述 投票:0回答:1

我面临 2 个问题

CollectionView
:

  1. 我已经为
    RemainingItemsThreshold="0"
    给出了
    CollectionView
    ,但是
    RemainingItemsThresholdReached
    事件在到达最后一个项目之前触发。它正在调用最后一个项目之前的大约 6 个项目。
  2. RemainingItemsThresholdReached
    被触发时,我加载下一组数据并将其附加到
    CollectionView
    。但此时,
    CollectionView
    滚动到第一项。它不会停留在我们加载更多数据的最后一项上。

我的收藏查看代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    BackgroundColor="White"
    x:Class="NeedHelp.Pages.FriendDetailsPage">

    <ContentPage.Content>
        <StackLayout>
            <Grid BackgroundColor="#eeeeee">
                
            </Grid>

            <ScrollView 
                x:Name="frienddetails_scrollview"
                IsVisible="False"
                Orientation="Vertical" 
                VerticalOptions="FillAndExpand" 
                HorizontalOptions="FillAndExpand">
                
                <StackLayout
                    Margin="15">
                    
                    <Frame 
                        BorderColor="#efefef"
                        BackgroundColor="White"
                        CornerRadius="5"
                        HasShadow="False"
                        Padding="5">
                        
                    </Frame>

                    <Frame
                        BorderColor="#efefef"
                        Padding="3"
                        x:Name="map_layout"
                        Margin="0,5,0,0">
                        
                    </Frame>

                    <Label
                        Margin="0,5,0,0"
                        x:Name="History_label"
                        Text="History"
                        TextColor="#424242"
                        HorizontalOptions="Center"
                        HorizontalTextAlignment="Center">
                    </Label>

                    <Frame
                        x:Name="Tab_layout"
                        BorderColor="#efefef"
                        Margin="0,5,0,0"
                        BackgroundColor="White"
                        HasShadow="False"
                        CornerRadius="5"
                        Padding="5">

                    </Frame>

                    <ListView 
                        x:Name="historylistview"
                        SeparatorVisibility="None"
                        HasUnevenRows="True"
                        SelectionMode="None"
                        CachingStrategy="RecycleElement"
                        BackgroundColor="#ffffff">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.View>
                                        <StackLayout
                                            Margin="0,0,0,10">
                                            
                                        </StackLayout>
                                    </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>

                        <ListView.Footer>
                            <Label/>
                        </ListView.Footer>
                    </ListView>

                    <CollectionView 
                        x:Name="MyTweetsList" 
                        Margin="0,5,0,5"
                        ItemsSource="{Binding AllItems,Mode=TwoWay}"
                        RemainingItemsThreshold="0"
                        RemainingItemsThresholdReached="LoadMoreTweets"
                        HorizontalOptions="Fill">
                        <CollectionView.ItemTemplate>
                            <DataTemplate>
                                <StackLayout
                                    x:Name="Item"
                                    HorizontalOptions="Fill"
                                    VerticalOptions="FillAndExpand"
                                    Orientation="Vertical">

                                </StackLayout>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
                        <CollectionView.HeightRequest>
                            <OnIdiom x:TypeArguments="x:Double">
                                <OnIdiom.Phone>400</OnIdiom.Phone>
                                <OnIdiom.Tablet>600</OnIdiom.Tablet>
                                <OnIdiom.Desktop>400</OnIdiom.Desktop>
                            </OnIdiom>
                        </CollectionView.HeightRequest>
                    </CollectionView>

                    <Label
                        VerticalOptions="CenterAndExpand"
                        HorizontalOptions="CenterAndExpand"
                        IsVisible="{Binding ComingSoonVisibility}"
                        HorizontalTextAlignment="Center"
                        Text="No Messages Yet."
                        x:Name="no_announcement_label"/>
                </StackLayout>
            </ScrollView>
            
            <Grid 
                x:Name="tweetBox"
                IsVisible="False"
                Margin="0,0,0,10">

            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

更新

我已经针对这个问题创建了一个demo

在此演示中,我最初加载 50 个项目,然后加载 10 个项目。加载最后 10 个后,collectionview 滚动到顶部并显示初始项目。

maui collectionview
1个回答
0
投票

您的主要问题:

这条线

AllItems = new ObservableCollection<UserTweetsList>(Items);

导致您的整个集合视图刷新。 可观察集合的全部目的是对列表进行更改。除了其他性能问题之外,重新创建集合也是导致滚动到开头的原因。

您的解决方案:

仅使用

AllItems = new
一次。 然后在加载项目时使用
AllItems.Add(..)

例如

if (!loadMore)
{
   Items.Add(new UserTweetsList() { tweetData = "message 1", tweetUser = "Albert});
   ...
   Items.Add(new UserTweetsList() { tweetData = "message 50", tweetUser = "Albert"});
   AllItems = new ObservableCollection<UserTweetsList>(Items); << move here
}
else
{
    AllItems.Add(new UserTweetsList() { tweetData = "message 51", tweetUser = "Albert" }); << change those lines to AllItems.Add not Items.Add
© www.soinside.com 2019 - 2024. All rights reserved.