Xamarin.Forms Import CarouselView into a XAML Page


Example

The basics

In the heading of ContentPage, insert following line:

xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"

Between the <ContentPage.Content> tags place the CarouselView:

<cv:CarouselView x:Name="DemoCarouselView">
</cv:CarouselView>

x:Name will give your CarouselView a name, which can be used in the C# code behind file. This is the basics you need to do for integrating CarouselView into a view. The given examples will not show you anything because the CarouselView is empty.

Creating bindable source

As example of an ItemSource, I will be using a ObservableCollection of strings.

public ObservableCollection<TechGiant> TechGiants { get; set; }

TechGiant is a class that will host names of Technology Giants

public class TechGiant
{
    public string Name { get; set; }

    public TechGiant(string Name)
    {
        this.Name = Name;
    }
}

After the InitializeComponent of your page, create and fill the ObservableCollection

TechGiants = new ObservableCollection<TechGiant>();
TechGiants.Add(new TechGiant("Xamarin"));
TechGiants.Add(new TechGiant("Microsoft"));
TechGiants.Add(new TechGiant("Apple"));
TechGiants.Add(new TechGiant("Google"));

At last, set TechGiants to be the ItemSource of the DemoCarouselView

DemoCarouselView.ItemsSource = TechGiants;

DataTemplates

In the XAML - file, give the CarouselView a DataTemplate:

<cv:CarouselView.ItemTemplate>
</cv:CarouselView.ItemTemplate>

Define a DataTemplate. In this case, this will be a Label with text bind to the itemsource and a green background:

<DataTemplate>
    <Label Text="{Binding Name}" BackgroundColor="Green"/>
</DataTemplate>

That's it! Run the program and see the result!