BUG: Can't add multiple items to ListView (ArgumentOutOfRangeException)

ChaseFlorellChaseFlorell CAInsider, University mod
edited July 2014 in Xamarin.Forms

I have confirmed this with both v1.1.1 and v1.2.1

Repro

  1. create new PCL project
  2. update Xamarin.Forms
  3. copy the below code into your App.cs
  4. run

    public class App
    {
        public static Page GetMainPage()
        {
            return new NavigationPage(new RootPage());
        }
    }
    
     public class RootPage : ContentPage
     {
        private ListView _listView;
    
        public RootPage()
        {
            var vm = new RootPageViewModel();
            Content = BuildListView(vm);
        }
    
        private StackLayout BuildListView(RootPageViewModel viewModel)
        {
            var headerTemplate = new DataTemplate(typeof(ModuleMediaListHeaderTemplate));
            headerTemplate.CreateContent();
    
            var itemTemplate = new DataTemplate(typeof(ModuleMediaListItemTemplate));
            itemTemplate.CreateContent();
    
            _listView = new ListView
            {
                ItemsSource = viewModel.MediaSections,
                IsGroupingEnabled = true,
                GroupDisplayBinding = new Binding("SectionName"),
                HasUnevenRows = false,
                GroupHeaderTemplate = headerTemplate,
                ItemTemplate = itemTemplate
            };
    
            return new StackLayout { Children = { _listView } };
        }
    }
    
    public class RootPageViewModel
    {
        public IEnumerable MediaSections
        {
            get
            {
                var titles = new[] {"First", "Second", "Third", "Forth", "Fifth"};
    
                return titles.Select(section => new MediaListSection(section)
                {
                    new FooViewModel {Title = "Foo", Description = "description for foo"}, 
                    new FooViewModel {Title = "Bar", Description = "description for bar"}, 
                    new FooViewModel {Title = "Baz", Description = "description for baz"}, 
                    new FooViewModel {Title = "Fiz", Description = "description for fiz"}, 
                    new FooViewModel {Title = "Buz", Description = "description for buz"},
                }).ToList();
            }
        }
    }
    
    public class MediaListSection : ObservableCollection<FooViewModel>
    {
        public string SectionName { get; private set; }
    
        public MediaListSection(string sectionName)
        {
            SectionName = sectionName;
        }
    }
    
    public class FooViewModel
    {
        public string Title { get; set; }
        public string Description { get; set; }
    }
    
    public class ModuleMediaListItemTemplate : ViewCell
    {
        public ModuleMediaListItemTemplate()
        {
            var title = new Label { YAlign = TextAlignment.Center };
            title.SetBinding(Label.TextProperty, new Binding("Title", BindingMode.OneWay));
    
            var description = new Label { YAlign = TextAlignment.Center };
            description.SetBinding(Label.TextProperty, new Binding("Description", BindingMode.OneWay));
    
            View = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                Padding = new Thickness(8),
                Children = { title, description } 
            };
        }
    }
    
    public class ModuleMediaListHeaderTemplate : ViewCell
    {
        public ModuleMediaListHeaderTemplate()
        {
            var title = new Label { TextColor = Color.White, YAlign = TextAlignment.Center };
            title.SetBinding(Label.TextProperty, new Binding("SectionName", BindingMode.OneWay));
    
            View = new StackLayout
            {
                Padding = new Thickness(8, 0),
                BackgroundColor = Color.FromHex("#999999"),
                Orientation = StackOrientation.Horizontal,
                Children = { title },
            };
        }
    }
    

Posts

  • ChaseFlorellChaseFlorell CAInsider, University mod

    Forgot to mention, this is on Android.

  • CraigDunnCraigDunn USXamarin Team Xamurai

    when I run this on Android 4.2 emulator it works fine (although seems to be displaying a cell-reuse bug with respect to the grey background). The project defaults to target Android 4.0.3. This is with a very old Xamarin.Forms (1.0.6186) which is what the 'file>new' template still ships with.

    when I upgraded to Xamarin Forms 1.2.1.6229 it fails with the error you describe. turning IsGroupingEnabled off prevents the problem occurring, but obviously does not then display the list correctly.

    looks like a bug/regression in the Android grouping code (since it runs fine on iOS on both the older and new versions of Xamarin.Forms).

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited July 2014

    @CragDunn Are you able to post a bug report? I'm not sure if my bug reports are ending up in front of the eyes of ppl who can fix them.

    all my bug reports have a little lock beside them, and others do not

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited July 2014

    @CragDunn also, that gray background bug is something else I've experienced but haven't reported. If you explicitly set a background color to the ItemTemplate, it goes away.

  • CraigDunnCraigDunn USXamarin Team Xamurai
    edited July 2014

    @ChaseFlorell‌ ack, this is the same issue as this

  • EricMaupinEricMaupin USXamarin Team Xamurai
    edited July 2014

    The exception has been fixed, it will go out with the next release.

    Side note, not the cause of bug:

    GroupDisplayBinding = new Binding("SectionName"),
    GroupHeaderTemplate = headerTemplate,
    

    These are mutually exclusive, you should see a warning about it in your output. GroupDisplayBinding is a simple way of, in this case, basically saying:

    GroupHeaderTemplate = new DataTemplate (typeof(TextCell)) { 
         Bindings = { { TextCell.TextProperty, new Binding ("SectionName") } } };
    
Sign In or Register to comment.