Bug 26495 - System.ArgumentOutOfRangeException when items removed, then added to ObservableCollection
Summary: System.ArgumentOutOfRangeException when items removed, then added to Observab...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-01-28 05:52 UTC by Peter Major
Modified: 2015-02-07 12:37 UTC (History)
6 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Peter Major 2015-01-28 05:52:28 UTC
iOS 8.1.2 
Model NE433B/A (iPhone 5s)


Xamarin Insights is caputuring a fairly common crash in our iOS Xamarin Forms application. There is a ListView which has an ItemsSource bound to an ObservableCollection.

If that collection has items, then items are removed until the list is empty, then items added again, the application crashes

Here is the exception information:

System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
  at Xamarin.Forms.ListProxy.get_Item (Int32 index) [0x00000] in <filename unknown>:0
  at Xamarin.Forms.TemplatedItemsList`2[Xamarin.Forms.ItemsView`1[Xamarin.Forms.Cell],Xamarin.Forms.Cell].get_Item (Int32 index) [0x00000] in <filename unknown>:0
  at Xamarin.Forms.Platform.iOS.ListViewRenderer+ListViewDataSource.GetCellForPath (MonoTouch.Foundation.NSIndexPath indexPath) [0x00000] in <filename unknown>:0
  at Xamarin.Forms.Platform.iOS.ListViewRenderer+ListViewDataSource.GetCell (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00000] in <filename unknown>:0
  at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)
  at MonoTouch.UIKit.UITableView.EndUpdates () [0x00008] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/UITableView.g.cs:255
  at Xamarin.Forms.Platform.iOS.ListViewRenderer.UpdateItems (System.Collections.Specialized.NotifyCollectionChangedEventArgs e, Int32 section, Boolean resetWhenGrouped) [0x00000] in <filename unknown>:0
  at Xamarin.Forms.Platform.iOS.ListViewRenderer+<>c__DisplayClass5.<OnCollectionChanged>b__4 () [0x00000] in <filename unknown>:0
  at MonoTouch.Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSAction.cs:164
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:45
  at JustEat.Ordertracker.Touch.Application.Main (System.String[] args) [0x00008] in /Users/peter.major/repo/rapps/JE.Ordertracker.Forms/JustEat.Ordertracker.Touch/Main.cs:19

This looks similar to this bug which is marked as resolved:
https://bugzilla.xamarin.com/show_bug.cgi?id=23778
Comment 1 Dominic N [MSFT] 2015-02-02 15:04:56 UTC
In my tests, this has been fixed with 1.3.2-pre3.

Closing bug.
Comment 2 Paul Hazlett 2015-02-07 12:11:45 UTC
I'm having this very same (or very similar) problem with XForms 1.3.2.6316.

See my post dated 05:40am
http://forums.xamarin.com/discussion/30608/crash-when-removing-items-from-observablecollection-in-xforms-1-3

Using source control to revert back to the last stable changeset reveals that as soon as I upgraded to XLabs 2.0.0 the problem cropped up. My XAML layout is slightly complicated in that each row is horizontally scrollable with the actual content for each row being build in my codebehind.

My XAML file is structured liked this:

<ListView
    IsGroupingEnabled="true"
    HasUnevenRows="true"
    GroupDisplayBinding="{Binding Key}"
    ItemSource="{Binding Items}">

    <ListView.GroupHeaderTemplate>
        <DataTemplate>
            <ViewCell>...</ViewCell>
        </DataTemplate>
    </ListView.GroupHeaderTemplate>

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.Cell>
                    <ContentView BindingContext="RowBindingContextChanged" />
                </ViewCell.Cell>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>


In my codebehind RowBindingContextChanged() I create a horizontal ScrollView.


private void OnRowBindingContextChanged(object sender, EventArgs e)
{
    ContentView view = (ContentView)sender;

    ... create the two layout ....

    view.Content = new ScrollView {
        Orientation = ScrollOrientation.Horizontal,
        Content = new StackLayout {
            Orientation = StackLayoutOrientation.Vertical,
            Children = {
                new Image() { ... setup image ...},
                new Label() { ... setup top row-label ...},
                new Label() { .... setup bottom row label ...}
            }
        }
    };
}
Comment 3 Paul Hazlett 2015-02-07 12:15:53 UTC
I forgot to include my build information:

=== Xamarin Studio ===

Version 5.7 (build 661)
Installation UUID: 2ef9f102-8c9a-425a-aa7b-e14829020578
Runtime:
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.6.0.51 (Indie Edition)
Hash: dfb682f
Branch: 
Build date: 2015-01-08 13:39:32-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Indie Edition)
Android SDK: /Users/phazlett/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0.3 (API level 15)
		4.4   (API level 19)
Java SDK: /usr
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 507000661
Git revision: b70bab61da996da29045ea8ee8aed1a6faedbe78
Build date: 2015-01-05 16:31:31-05
Xamarin addins: 82f6c71490562d6cd125a09287f441902fdac3d7

=== Operating System ===

Mac OS X 10.10.1
Darwin Pauls-iMac.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 4 Paul Hazlett 2015-02-07 12:37:29 UTC
I just figured out that removing the application stops crashing when I ScrollView from my ViewCell layout.

So my code behind function looks like this:

private void OnRowBindingContextChanged(object sender, EventArgs e)
{
    ContentView view = (ContentView)sender;

    ... create the the layout ....

    view.Content = new StackLayout {
        Orientation = StackLayoutOrientation.Vertical,
        Children = {
            new Image() { ... setup image ...},
            new Label() { ... setup top row-label ...},
            new Label() { ... setup bottom row label ...}
        }
    };
}

The problem is that I need the ListView rows to be horizontally scrollable, so this isn't a solution or workaround.