Xamarin.Forms CarouselView

As with any app that has a need to display products or services, or show off its new features, you're more than likely going to want a carousel view within your app, its a staple of UI that you'd think Xamarin would have by default right?

Wrong. Whilst Xamarin does have inbuilt support for a CarouselView, you're going to be pretty disappointing when you realise that you can't run your app without enabling experimental features or installing their beta NuGet package. I'm not sure how you feel about it, but this makes me feel sick knowing I'm about to release an app with features that might brick it for every user. Nonetheless here's an example of how to use it and then we'll go into why you might want to install a third party package to get things done right.

Example:

Before calling Forms.Init within the platform specific code for both Android and iOS:

Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");

Within the page where the CarouselView is required.

CarouselView objLayout = new CarouselView()
{
    HorizontalOptions   = LayoutOptions.FillAndExpand,
    VerticalOptions     = LayoutOptions.FillAndExpand,
    ItemTemplate        = new DataTemplate(typeof(ImageCell)),
    ItemsSource         = colImages
};

From there on out, it works just like a regular ListView, you're just held back by the issues mentioned above. Note that before you can even run the app, you need to set the 'CollectionView_Experimental' flag for Xamarin.

Here's a useful article on Stack Overflow describing th issues encountered on UWP.

Other options available:

Syncfusion:
This is a really great package and has th full backing of Syncfusion, so you know when installing this package, you're going to get quality and there's great documentation and support on hand to help out.

https://www.syncfusion.com/xamarin-ui-controls/xamarin-carousel-view

CarouselView.FormsPlugin:
Another great option if you want to support those open source projects out there, just like Syncfusion, this package has support for navigation dots/buttons, scaling, custom templates and data-sources.

https://github.com/alexrainman/CarouselView

Example:

After Forms.Init within the platform specific project from both Android and iOS:

CarouselViewRenderer.Init();

Within the page where the CarouselViewControl is required.

CarouselViewControl objLayout = new CarouselViewControl()
{
    HorizontalOptions   = LayoutOptions.FillAndExpand,
    VerticalOptions     = LayoutOptions.FillAndExpand,
    ItemTemplate        = new DataTemplate(typeof(ImageCell)),
    ItemsSource         = colImages,
    InterPageSpacing    = 0,
    ShowIndicators      = true,
    IndicatorsTintColor = Color.White,
    CurrentPageIndicatorTintColor = Core.Colours.PinkAccent,
    Position            = intStartIndex
};

Published at

Tags: C#,Xamarin,Xamarin Forms,Android,iOS,App

Luke Alderton

Comments

Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By Ben on Raspberry Pi - Running Java app on Raspbian
24 May 2020, 09:01 AM
By BP on Disabling XAML Hot Reload
14 May 2020, 21:59 PM
By Andy on Xamarin Forms - Working with threads
12 May 2020, 09:16 AM
By mitchell on Xamarin forms - Stop/Cancel back button event
29 Apr 2020, 18:16 PM
By Paolo on Raspberry Pi - Running Java app on Raspbian
19 Apr 2020, 11:04 AM
By Alexander Gontar on Xamarin Forms - Working with threads
13 Mar 2020, 07:10 AM
By Arthur Richardson on Disabling XAML Hot Reload
12 Mar 2020, 17:52 PM
By George on Xamarin Forms - Working with threads
10 Mar 2020, 05:04 AM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development