Xamarin Forms - Ordered List and ArgumentOutOfRange

Viewed 781

I've been fighting with this exception (and subsequent hard crash) for about a day now. I have a ListView in Xamarin Forms that does grouping headers. As much as I can gather from the Call Stack, the error is occurring because of the grouping. I verified this by removing the grouping from the ListView and the app displays the data without crashing. I've even diff'd the code back a few weeks and nothing stands out. This also only occurs on a new launch and subsequent launches don't throw this exception or crash.

How else might I go about further debugging and resolving this?

Exception stack trace

List View XAML

<ListView x:Name="ListViewItems"
            ItemsSource="{Binding ItemsGrouped}"
            GroupDisplayBinding="{Binding Key.DisplayText}"
            IsGroupingEnabled="true">
    <ListView.GroupHeaderTemplate>
        <DataTemplate>
            <ViewCell>
                <cells:MyGroupHeaderView/>
            </ViewCell>
        </DataTemplate>
    </ListView.GroupHeaderTemplate>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <cells:ItemCellView />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

MyGroupHeaderView XAML

<StackLayout VerticalOptions="Center">
    <Label Text="{Binding Key.DisplayText}">
    </Label>
</StackLayout>

Grouping C# Code

var result = from s in myItems
    orderby s.ItemDateTime
    group s by s.GetSortGroupInfo()
    into itemGroup
    select new Grouping<GroupInfo, ItemViewModel>(itemGroup.Key, itemGroup);

GroupInfo class (used for sorting and display)

public class GroupInfo
{
    public string DisplayText { get; set; }

    public int SortValue { get; set; }
}

Bound Property and Setter

public ObservableRangeCollection<Grouping<GroupInfo, ItemViewModel>> ItemsGrouped { get; }
    = new ObservableRangeCollection<Grouping<GroupInfo, StopViewModel>>();

this.ItemsGrouped.ReplaceRange(
    this.MyItems.GroupByDate()
        .OrderBy(f => f.Key.SortValue)
        .ToList());
2 Answers
Related