Category / Section
How to render a Carousel like Xamarin Carousel Page
1 min read
Xamarin CarouselPage provides a support to swipe the pages. We can achieve the same by using Syncfusion SfCarousel control with Linear mode. The default mode of carousel is “Default”. The below code illustrates the way to achieve this.
C#
public partial class CarouselFeatureSamplePage : ContentPage { public CarouselFeatureSamplePage() { //Initialize the carousel control SfCarousel sfCarousel = new SfCarousel(); if (Device.OS == TargetPlatform.Android) { sfCarousel.ItemWidth = 360; sfCarousel.ItemHeight = 550; } if (Device.OS == TargetPlatform.iOS) { sfCarousel.ItemWidth = 360; sfCarousel.ItemHeight = 530; } // To obtain the carouselPage view need to use Linear mode of carousel sfCarousel.ViewMode = ViewMode.Linear; ObservableCollection<SfCarouselItem> collectionOfItems = new ObservableCollection<SfCarouselItem>(); StackLayout firstItem = new StackLayout() { BackgroundColor = Color.Green }; firstItem.Children.Add(new Label() { Text = "CarouselPage 1", FontSize = 20, TextColor = Color.White, HorizontalOptions = LayoutOptions.CenterAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand }); StackLayout secondItem = new StackLayout() { BackgroundColor = Color.Red }; secondItem.Children.Add(new Label() { Text = "CarouselPage 2", FontSize = 20, TextColor = Color.White, HorizontalOptions = LayoutOptions.CenterAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand }); StackLayout thirdItem = new StackLayout() { BackgroundColor = Color.Blue }; thirdItem.Children.Add(new Label() { Text = "CarouselPage 3", FontSize = 20, TextColor = Color.White, HorizontalOptions = LayoutOptions.CenterAndExpand, VerticalOptions = LayoutOptions.CenterAndExpand }); collectionOfItems.Add(new SfCarouselItem() { ItemContent = firstItem, HeightRequest = 900, WidthRequest = 900 }); collectionOfItems.Add(new SfCarouselItem() { ItemContent = secondItem }); collectionOfItems.Add(new SfCarouselItem() { ItemContent = thirdItem }); sfCarousel.DataSource = collectionOfItems; this.Content = sfCarousel; } }