Forum Cross Platform with Xamarin

InfiniteSimpleListView crashes into IOS version

garbelini_paulogarbelini_paulo Member ✭✭

Hi guys! newbiew xamarin here.

I having some trouble with my InfiniteSimplesListView.
Xamarin.forms version -> 4.8.0.1451

My pages with InfiniteSimplesListView run with loadmorecommand and RecycleElement, bringing 30 rows per loadmore.

In Android, everything works fine.
In IOS, when I enter in a page with this listview, it appears that he load more and more content freezing the application or warning 'Expression tree is too large'

Someone can help me ?
Sorry about my english

this is my InfiniteSimpleListView

public class InfiniteSimpleListView : ListView
{
///
/// Respresents the command that is fired to ask the view model to load additional data bound collection.
///
public static readonly BindableProperty LoadMoreCommandProperty = BindableProperty.Create<InfiniteSimpleListView, ICommand>(bp => bp.LoadMoreCommand, default(ICommand));
    /// <summary>
    /// Gets or sets the command binding that is called whenever the listview is getting near the bottomn of the list, and therefore requiress more data to be loaded.
    /// </summary>
    public ICommand LoadMoreCommand
    {
        get { return (ICommand)GetValue(LoadMoreCommandProperty); }
        set { SetValue(LoadMoreCommandProperty, value); }
    }

    /// <summary>
    /// Creates a new instance of a <see cref="InfiniteSimpleListView" />
    /// </summary>
    public InfiniteSimpleListView()
    {
        try
        { 
            if (Device.RuntimePlatform != Device.iOS)
            {
                this.SetBinding(IsRefreshingProperty, "IsBusy");
            }
            ItemAppearing += InfiniteSimpleListView_ItemAppearing;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public InfiniteSimpleListView(ListViewCachingStrategy strategy) : base(strategy)
    {
        try
        {
            if (Device.RuntimePlatform != Device.iOS)
            {
                this.SetBinding(IsRefreshingProperty, "IsBusy");
            }
            ItemAppearing += InfiniteSimpleListView_ItemAppearing;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


    void InfiniteSimpleListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
    {
        try
        {
            var items = ItemsSource as IList;

            if (items != null && e.Item == items[items.Count - 1])
            {
                if (LoadMoreCommand != null && LoadMoreCommand.CanExecute(null))
                    LoadMoreCommand.Execute(null);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}

This is my XAML

<custom:InfiniteSimpleListView x:Name="ListViewDados" Grid.Row="1" ItemsSource="{Binding LItens}"
                                 SelectedItem="{Binding ItemSelected}"                                       
                                 LoadMoreCommand="{Binding LoadItensCommand}"
                                  ItemSelected="ListView_OnItemSelected" >
            <x:Arguments>
                <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
            </x:Arguments>
            <!--<ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>-->
            <custom:InfiniteSimpleListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Display }" Detail="{Binding Detail}" TextColor="{StaticResource CinzaEscuro}" DetailColor="{StaticResource CinzaApresentacaoCliente}">
                        <TextCell.ContextActions>
                            <MenuItem Text="Excluir" Clicked="MenuItem_OnClicked" />
                        </TextCell.ContextActions>
                    </TextCell>
                </DataTemplate>
            </custom:InfiniteSimpleListView.ItemTemplate>
        </custom:InfiniteSimpleListView>

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    What's the code in LoadItensCommand ? Could you provide the complete code so that we can reproduce the problem ?

    Btw , I suggest you take a look at InfiniteScrolling , a small library to quickly and easily add infinite/endless scrolling support to any Xamarin.Forms ListView.

    I test the sample the scrolling works smoothly without any freezing .

    The usage is pretty simple

    Adding infinite scrolling support to a ListView is done in two steps. First we add the InfiniteScrollBehavior to the ListView's behaviors:

    <!-- xmlns:scroll="clr-namespace:Xamarin.Forms.Extended;assembly=Xamarin.Forms.Extended.InfiniteScrolling" -->
    
    <!-- a normal list view -->
    <ListView CachingStrategy="RecycleElement" ItemsSource="{Binding Items}">
    
        <!-- the behavior that will enable infinite scrolling -->
        <ListView.Behaviors>
            <scroll:InfiniteScrollBehavior />
        </ListView.Behaviors>
    
        ...
    
    </ListView>
    

    And then, we need to make use of the InfiniteScrollCollection as our collection type (instead of ObservableCollection or List):

    using Xamarin.Forms.Extended.InfiniteScrolling;
    
    public class MainViewModel : BaseViewModel
    {
        private const int PageSize = 20;
    
        public MainViewModel()
        {
            Items = new InfiniteScrollCollection<DataItem>
            {
                OnLoadMore = async () =>
                {
                    // load the next page
                    var page = Items.Count / PageSize;
                    var items = await dataSource.GetItemsAsync(page + 1, PageSize);
    
                    // return the items that need to be added
                    return items;
                }
            };
        }
    
        public InfiniteScrollCollection<DataItem> Items { get; }
    }
    

    If we need to manually trigger the list to load more, for example to initialize the list, we can just use the LoadMoreAsync method:

    Items.LoadMoreAsync();
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.