Forum Xamarin.Forms

My carousel no change of position when i set the value of property from code

fmaster_icfmaster_ic Member ✭✭
edited December 2019 in Xamarin.Forms

Im have a carousel (https://github.com/alexrainman/CarouselView) where the user can see samples, these samples are added one by one, and the user could see the last one created each time that he press the button, in android the project works fine, but in iOS the carousel has a very strange behavior, because when user press button add(see below) in the carousel only appears the first and last elements in the item source :S

I attached some code to understand better this situation.

xmla page

<controls:CarouselViewControl x:Name="cvSamplings" ItemsSource="{Binding ListSamplings, Mode=TwoWay}" PositionSelected="cvSamplings_PositionSelected"
                                      Orientation="Horizontal" InterPageSpacing="10" Position="{Binding MyPosition,Mode=TwoWay}"                                        
                                      VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

code to change current position

var index = viewModel.AddSample();
BindCarouselView();
viewModel.MyPosition = index;

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    How do you add views from BindCarouselView(); and what does this index represent for?
    I created a project based on the sample above. It runs fine on iOS and we could reach the end of the carousel view:

  • fmaster_icfmaster_ic Member ✭✭
    edited December 2019

    @LandLu said:
    How do you add views from BindCarouselView(); and what does this index represent for?
    I created a project based on the sample above. It runs fine on iOS and we could reach the end of the carousel view:

    i've attached my project, please could you check my solution? thanks!

  • LandLuLandLu Member, Xamarin Team Xamurai

    I changed the type of ListSamplings from List to ObservableCollection and then comment out the BindCarouselView method in the btnAdd_Clicked:

    private void btnAdd_Clicked(object sender, EventArgs e)
    {
        var index = viewModel.AddSample();
        //BindCarouselView();
        viewModel.MyPosition = index;
    }
    

    This is becasue ObservableCollection implement the INotifyCollectionChanged interface it could aware of the changing of items. Therefore, we don't need to use BindCarouselView to reset the items source.
    I attached the modified project here.

  • VidhyaAnbazhaganVidhyaAnbazhagan USMember ✭✭

    I use Xamarin forms version of 4.6.0.1142 and the Carousel View does not react when the position is changed programmatically. But when swiped the contentview is shown up. Any ideas on it , Please help me.

Sign In or Register to comment.