Bug 40631 - Setting CarouselView.Position throw exception (v2.2.0.18-pre3)
Summary: Setting CarouselView.Position throw exception (v2.2.0.18-pre3)
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.2.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bryan Hunter
URL:
Depends on:
Blocks:
 
Reported: 2016-04-22 00:51 UTC by David CASBONNE
Modified: 2017-06-19 18:09 UTC (History)
6 users (show)

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


Attachments
Demo bug 40631 (74.27 KB, application/zip)
2016-04-25 02:42 UTC, David CASBONNE
Details


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 David CASBONNE 2016-04-22 00:51:04 UTC
In a CarouselView I need to set the starting item. To do that I have tried 3 different ways : via Binding in XAML, in the ContentPage constructor and in the ContentPage.OnAppearing()

Results are differents in iOS and Android.

Android (Nexus 5 in 5.0 Genymotion)
- Binding => KO (System.ArgumentOutOfRangeException - see below)
- Constructor => KO (System.ArgumentOutOfRangeException - see below)
- OnAppearing => OK

iOS 9.3 (simulator)
- Binding => OK
- Constructor => OK
- OnAppearing => OK

Code done :
- Binding : <CarouselView ItemsSource="{Binding Vehicles}" x:Name="xCarouselView" Position="{Binding Position}">
- Constructor : this.xCarouselView.Position = 2 (i've 3 items in my ItemsSource)
- OnAppearing : this.xCarouselView.Position = 2 (i've 3 items in my ItemsSource)

Exception for Android :
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: count
  at System.Linq.Enumerable.Range (Int32 start, Int32 count) [0x0001c] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:1144
  at Xamarin.Forms.Platform.Android.CarouselViewExtensions.ToRange (System.Tuple`2 startAndCount) [0x00000] in C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\CarouselViewRenderer.cs:92
  at Xamarin.Forms.Platform.Android.PhysicalLayoutManager.OnLayoutChildren (Android.Support.V7.Widget.Recycler recycler, Android.Support.V7.Widget.State state) [0x0003b] in C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\CarouselViewRenderer.cs:1046
  at Android.Support.V7.Widget.RecyclerView+LayoutManager.n_OnLayoutChildren_Landroid_support_v7_widget_RecyclerView_Recycler_Landroid_support_v7_widget_RecyclerView_State_ (IntPtr jnienv, IntPtr native__this, IntPtr native_recycler, IntPtr native_state) [0x00019] in <filename unknown>:0
  at at (wrapper dynamic-method) System.Object:af8ed00f-05bb-46ee-b8ff-e178315c0c10 (intptr,intptr,intptr,intptr)
Comment 1 Bryan Hunter 2016-04-24 02:53:08 UTC
Hi David. 

We are working on your issue now. As we write code to reproduce the problem we're concerned we may be missing parts of what you experienced. Would you please attach a reproduction project (zip)? 

Ideally, the reproduction would be created by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue. 

We may be able to solve without this, but it would definitely help the cause.

Thanks!
Comment 2 Rui Marinho 2016-04-24 20:57:21 UTC
Hi David we tried to reproduce the issue but without luck, here's the code snippet that we used:

public class Bugzilla40631 : TestContentPage
	{
		DemoViewModel vm;
		protected override void Init()
		{
			var carouselView = new CarouselView();
			carouselView.SetBinding(CarouselView.ItemsSourceProperty, "Messages");
			carouselView.SetBinding(CarouselView.PositionProperty, "Position");
			carouselView.ItemTemplate = new DataTemplate(typeof(ItemsView1));

			// Initialize ui here instead of ctor
			Content = carouselView;
			BindingContext = vm = new DemoViewModel();

		}

		public class ItemsView1 : ContentView
		{
			public ItemsView1()
			{
				Padding = 40;
				var label = new Label { Text = "Hello" };
				label.SetBinding(Label.TextProperty, "Subject");
				Content = label;
			}
		}

		internal class DemoViewModel : ViewModelBase
		{
			public DemoViewModel()
			{
				Messages = new ObservableCollection<MessageViewModel>();
				Messages.Add(new MessageViewModel { Subject = "Hello 1" });
				Messages.Add(new MessageViewModel { Subject = "Hello 2" });
				Messages.Add(new MessageViewModel { Subject = "Hello 3" });
				Position = 2;

			}

			public ObservableCollection<MessageViewModel> Messages
			{
				get;
				private set;
			}

			int _position = -1;
			public int Position
			{
				get
				{
					return _position;
				}
				set
				{
					_position = value;
					OnPropertyChanged(nameof(Position));
				}
			}
		}
}
Comment 3 David CASBONNE 2016-04-25 02:34:13 UTC
Hi,

If I start a new blank Xamarin.Forms solution I'm not able to have the 2.2.0-pre3 as the pre4 is presented in the packages manager...
So I've tried to "light-ify" my project to send you a repro but the bug wasn't there anymore !
I've tried in the real project and the bug is already there ^^

The difference is :
- in the demo project, I started the App with the "buggie" page
- in the real project, I started the App with a MasterDetailPage which has in its detail the "buggie" page.

I finally succeeded in creating a repro project but the bug is more related to the MasterDetailPage I think now...

In the demo project, in App.cs, you just have to (un)comment to see the bug :

// WITH the bug
MainPage = new MainPage();

// WITHOUT the bug
//MainPage = new HomePage();

In HomePage.cs, you can see :
// Bug is caused by this line...
this.xCarouselView.Position = 2;

I hope that would help you !
Comment 4 David CASBONNE 2016-04-25 02:42:10 UTC
Created attachment 15826 [details]
Demo bug 40631

I HAVE REMOVED ALL PACKAGES TO RESPECT 8Mo FILE SIZE. PROJECT WILL NOT COMPILE UNTIL YOU RESTORE IT :)

See https://bugzilla.xamarin.com/show_bug.cgi?id=40631#c3 for more informations
Comment 5 Samantha Houts [MSFT] 2017-06-19 18:09:26 UTC
This issue is not present in the newly proposed version of CarouselView. Closing this issue. Thank you!

https://github.com/xamarin/Xamarin.Forms/pull/853