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 tiky on JQuery Unobtrusive Validation for dynamically created elements
16 Aug 2019, 13:10 PM
By Gerry on Replacing the Xamarin Header/Navigation bar with a custom view/template
11 Jul 2019, 17:12 PM
By steve choi on Xamarin Forms multiple instances of same app open
30 Jun 2019, 09:09 AM
By esrgr on JQuery Unobtrusive Validation for dynamically created elements
11 Jun 2019, 06:01 AM
By annualmars on Raspberry Pi - Running Java app on Raspbian
10 May 2019, 15:48 PM
By Shahriar H. Razi on Xamarin Forms - Working with threads
7 May 2019, 09:25 AM
By Person on MVC forms in Umbraco
30 Apr 2019, 13:33 PM
By christopher on How to add a Xamarin Forms Loading Screen/Overlay
27 Mar 2019, 11:59 AM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development