Xamarin Forms - ordered list and OutOfRange argument

I have been fighting this exception (and the ensuing hard crash) for about a day. I have a ListView in Xamarin Forms that groups headers. As far as I can compile from Call Stack, the error is due to grouping. I checked this by removing the grouping from the ListView, and the application displays the data without crashing. I even canceled the code back a few weeks and nothing stands out. This also happens only on a new start, and subsequent launches do not throw this exception or crash.

How else can I continue debugging and solve this problem?

Exception stack trace

View XAML List

<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>

C # code grouping

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 displaying)

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

    public int SortValue { get; set; }
}

Associated 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());
+4
2

. , James Montemagno Xam.Plugin.Geolocator 3.04 4.01, PCL .NET Standard. , ListView . , GeoLocation 3.04, , -, , 4.01.

0

, TemplatedItemsList Reset, ReplaceRange.

, ReplaceRange:

ItemsGrouped.Clear();
foreach (var item in MyItems.GroupByDate().OrderBy(f => f.Key.SortValue))
{
    ItemsGrouped.Add(item);
}

:

<ListView x:Name="ListViewItems"
        ItemsSource="{Binding ItemsGrouped}"
        IsGroupingEnabled="true"
        CachingStrategy="RecycleElement">

, , UnhookItem, :

//Hack: the cell could still be visible on iOS because the cells are reloaded after this unhook 
//this causes some visual updates caused by a null datacontext and default values like IsVisible
if (Device.RuntimePlatform == Device.iOS && CachingStrategy == ListViewCachingStrategy.RetainElement)
    await Task.Delay(100);
item.BindingContext = null;

, , , ... , , Android...

GroupDisplayBinding, GroupHeaderTemplate ... , ... ... GroupDisplayBinding , .

+1

Source: https://habr.com/ru/post/1682910/


All Articles