How can I change the page programmatically in the Carousel in Xamarin.forms?

I am using the CarouselPage class to implement a horizontal slider in Xamarin.Forms. I'm going to make a CarouselPage class to navigate to the next page by clicking on the page instead of scrolling.

Is it possible? Can anyone help me?

Thanks in advance.

+3


source to share


1 answer


You can add TapGestureRecognizer to your page and connect it to change CurrentPage in CarouselPage. Below is an extension method I wrote to move the current index of the CarouselPage to the right. Calling this method from a TapGestureRecognizer attached to the child page should provide you with the functionality you want.



public static void PageRight(this CarouselPage carouselPage)
{
    var pageCount = carouselPage.Children.Count;
    if (pageCount < 2) 
        return;

    var index = carouselPage.Children.IndexOf(carouselPage.CurrentPage);
    index++;
    if (index >= pageCount) 
        index = 0;

    carouselPage.CurrentPage = carouselPage.Children[index];
}

      

+10


source







All Articles