Automatic Image Slider In Xamarin.Forms

In this article, you will learn creating automatic image slider in Xamarin.Forms.

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

  • Carousel View has the ability to show items in 3D or linear arrangements.
  • On-demand loading for efficient data utilization  -- no need to pre-populate a large number of items at the same time.
  • UI Virtualization helps reduce loading time and app’s live memory.

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.         
    3.  <Label Text="Carousel View"  HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />  
    4.   
    5.        <forms1:CarouselView x:Name="MainCarouselView">  
    6.            <forms1:CarouselView.ItemTemplate>  
    7.                <DataTemplate>  
    8.                    <Image Source="{Binding .}"></Image>  
    9.                </DataTemplate>  
    10.            </forms1:CarouselView.ItemTemplate>  
    11.        </forms1:CarouselView  
    12. 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
  • Go to your UWP project.
  • Type the following code in your “Xaml” File of the UWP project.
    1. <Application.Resources>  
    2.         <DataTemplate x:Key="ItemTemplate">  
    3.             <Platform:ItemControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Platform:ItemControl>  
    4.         </DataTemplate>    
    5.   </Application.Resources>  
  • The code will be typed inside the <Application> Tag of the “Xaml” file of UWP project.

    Automatic Image Slider In Xamarin.Forms

    Add the following library in “Library Files”.

    xmlns:Platform="using:Xamarin.Forms.Platform"

    Automatic Image Slider In Xamarin.Forms

    Now, with the help of this setting, we will be able to see the output on UWP.

Output

 
Automatic Image Slider In Xamarin.Forms