A common practice when initializing a CarouselView is to assign the ItemsSource and then set the initial selection with ScrollTo(object item) in the page constructor. This doesn't work.
InitializeComponent();
Carousel.ItemsSource = collection;
Carousel.ScrollTo(item);
...where Carousel is the x:Name assigned in XAML.
Carousel.Scrolled += (s, e) => Title = collection[e.CenterItemIndex]?.Name;
The ScrollTo() call should set the initial selected item to be _item_.
The first item in the collection ends up being selected and the ScrollTo() is ignored.
The workaround is to place the ScrollTo() call in an Appearing event handler. This, however, causes a messy sequence of selections, starting with the first item in the colleciton, followed by the second item, and then finally the target item. Animations make this very distracting.
Note that this would all be much simpler if CarouselView inherited from SelectedItemsView so that we could just bind the SelectedItem to a property in our ViewModel.
We are still working on CarouselView, but you just need to set the Position no need to call ScrollTo, Will that work for you?
Carousel.ItemsSource = collection;
Carousel.Position = 2;
I'm not seeing Carousel.Position in current builds, but I honestly wouldn't think to look for it. I'd expect SelectedIndex and/or SelectedItem like in CarouselPage, TabbedPage, Picker and ListView. Does any other control have a Position property?
In fact, SelectedItem is much more useful for the MVVM design pattern.
Hi there's a Position and CurrentItem it works fine with MVVM patterns..
Hi,
I don't have a Positionand CurrentItemeither and I'm at the latest Xamarin Version thats available for me. 4.2.0.709249 at the moment and there is no update.
I'm having the same problem with the ScrollTo() when initializing the component.
@Julingska Please try 4.3.0-pre1. Thank you!
Bug is still present in 4.3.0.778476-pre1.
@samhouts @Julingska : Bug still present in 4.3.0.851321-pre3 when using ScrollTo(). However, you can now use CurrentItem successfully in the constructor.
@Magendanz Can you please attach a small project that demonstrates this issue? Thanks!
Are you unable to reproduce? I thought the code segments above were pretty clear.
Taken the liberty to create a reproduction based on the code provided. Does indeed not seem to work while setting Position does. Both on iOS and Android.
So, I think the underlying problem here is with animation. This is on by default, so just calling ScrollTo(item) will attempt to animate to the target item. For me this usually fails.
However, if you call with the animate parameter set to false, it works as expected.