Introduction

In this article, you will learn how to create an image slideshow automatically. This can be done by using “Carousel View”.

What is Carousel View

Steps for Creating Image Slider

  1. We will have to install a NuGet package which will help us to create “Image Slider” in Xamarin. Forms. The name of the NuGet package is given below.

    Forms.CarouselView”.
  1. After installing the NuGet package, add the following library in your library files which will help you to work with Carousel View.

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

    Automatic Image Slider In Xamarin.Forms
  1. Now, it’s time to type some lines of code.

    Type the following code in the XAML file, which is defined below.
    1. <StackLayout>
    2. <Label Text="Carousel View" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
    3. <forms1:CarouselView x:Name="MainCarouselView">
    4. <forms1:CarouselView.ItemTemplate>
    5. <DataTemplate>
    6. <Image Source="{Binding .}"></Image>
    7. </DataTemplate>
    8. </forms1:CarouselView.ItemTemplate>
    9. </forms1:CarouselView
    10. lt;/StackLayout>
  1. Type the following code in your “cs” file.
    1. var images = new List<string>
    2. {
    3. "https://image.shutterstock.com/image-photo/beautiful-abstract-grunge-decorative-navy-260nw-539880832.jpg","http://localhost:62212/Uploads/back2.png","http://localhost:62212/Uploads/heart.png"
    4. };
    5. MainCarouselView.ItemsSource = images;
    This code will be written under the “Initialize component();”

    Automatic Image Slider In Xamarin.Forms

    We’ll give some “Image Addresses” in our code on which we want to apply the Image Slider.
  1. Now, if we want our Image Slider to work automatically, we’ll have to type some more code in our “cs” file, as defined below.
    1. Device.StartTimer(TimeSpan.FromSeconds(5), (Func<bool>)(() =>
    2. {
    3. MainCarouselView.Position = (MainCarouselView.Position + 1) % images.Count;
    4. return true;
    5. }));
    This code will help us to work with image slider automatically.

    Automatic Image Slider In Xamarin.Forms

    With the help of this code, we can get an output on Android successfully. But if we want to get output on UWP, we have to do some extra settings for getting the output.
  1. Settings for UWP

Output

Automatic Image Slider In Xamarin.Forms