Delpin Susai Raj Thursday 23 August 2018

Xamarin.Forms - Image Slider using Image Carousel Plugin

In this blog,  you will learn how to slide images Using Image Carousel in Xamarin forms. 



Introduction


Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Image Carousel

Image Carousel is users can swipe from side to side to swipe images, like a gallery. This article demonstrates how to use a Image Carousel to swipe images through a collection of images.

Prerequisites

  • Visual Studio 2017(Windows or Mac)
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project.  you’ll learn more by going through the steps yourself.
Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.



Name your app, select “Use Portable Class Library” for shared code, and target both Android and iOS.


You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.


You now have a basic Xamarin.Forms app. Click the play button to try it out.

Add Image Carousel Plugin


In this step, add Image Carousel to your project. You can install Image Carousel via NuGet, or you can browse the source code on GitHub.

Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "Xamd.Plugins.Forms.ImageCarousel" and add Package. Remember to install it for each project (PCL, Android, iO, and UWP).



Image Carousel requires platform-specific setup

Android

In the Android project's MainActivity that is launched Image Carousel must be initialized in the OnCreate method.

MainActivity.cs

ImageCarouselRenderer.Init();

iOS

In the iOS project's Image Carousel must be initialized in the FinishedLaunching method on AppDelegate.

AppDelegate.cs

ImageCarouselRenderer.Init();


Add xmlns namespace


MainPage.xaml

Setting up the User Interface


Go to MainPage.Xaml and write the following code.

MainPage.xaml


In this step, write the following code for slide images using ImageCarousel.


MainPage.xaml.cs

using Xamarin.Forms;
using Xamd.ImageCarousel.Forms.Plugin.Abstractions;
namespace XamarinImageSlider
{
    public partial class MainPage : ContentPage
    {
        ObservableCollection imageSources = new ObservableCollection();
        public MainPage()
        {
            InitializeComponent();

            //Create a collection of ImageSources
            imageSources.Add("XamarinmonkeyLogo.png");
            imageSources.Add("github.png");
            imageSources.Add("microsoft.png");


            imgSlider.Images = imageSources;
        }
    }
}



Click the play button to try it out.



Download source from GitHub Repository


I hope you have understood how to  slide images Using Image Carousel in Xamarin.Forms.
Thanks for reading. Please share comments and feedback.

3 comments:

  1. How to make this slider automatic.show it displays all the images in circular way in a particular interval.

    ReplyDelete
  2. I just realize it just accept file image sources but not uri image sources, so please make sure you actually intrested in file image sources

    ReplyDelete
  3. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Advanced Scrum Master during this lockdown period everyone can use it wisely.
    Advanced Scrum Master Training Online

    ReplyDelete