Xamarin.forms: [Bug] Unable to set initial CarouselView item using ScrollTo() in page constructor

Created on 24 Aug 2019  路  11Comments  路  Source: xamarin/Xamarin.Forms

Description

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.

Steps to Reproduce

  1. In the page constructor, initialize the CarouselView as follows:
InitializeComponent();

Carousel.ItemsSource = collection;
Carousel.ScrollTo(item);

...where Carousel is the x:Name assigned in XAML.

  1. Track SelectionChanged events with the following:
Carousel.Scrolled += (s, e) => Title = collection[e.CenterItemIndex]?.Name;
  1. See which items get selected.

Expected Behavior

The ScrollTo() call should set the initial selected item to be _item_.

Actual Behavior

The first item in the collection ends up being selected and the ScrollTo() is ignored.

Basic Information

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.

  • Version with issue: v4.2.0.709249
  • Last known good version: N/A
  • IDE: VS 2019 v16.2.3
  • Platform Target Frameworks:

    • iOS: Untested

    • Android: 8.1 (API 27)

  • Android Support Library Version: 9.4.1.0

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.

carouselview 2 help wanted bug up-for-grabs

All 11 comments

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.

Repro7268.zip

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EdoardoCinelli picture EdoardoCinelli  路  92Comments

hartez picture hartez  路  55Comments

kingces95 picture kingces95  路  89Comments

jassmith picture jassmith  路  199Comments

hartez picture hartez  路  178Comments