Carousel Page In Xamarin.Forms

The following topics will be discussed in this article.

  • Carousel page
  • Uses of Carousel page
  • Practical example of Carousel page

Targeted Audience

Targeted audience is people with basic knowledge of XAML and C#.

What Carousel page is

In your mobile, you may use some application that contains different navigation between pages by a swipe gesture. You may touch and swipe to move to next page and you can navigate from one page to another page by swipe gesture which is swiped from ‘left to right’ or ‘right to left’. Carousel page is a page that contains different pages and each page has its own content depending upon the type and logic of application.

Xamarin

You can also see it in the image. Let’s discuss some of its uses and move towards its practical example.

Uses of a Carousel page

  • Access multiple pages by a swipe gesture
  • Navigation between pages become easy by swiping
  • Easy to use
  • One of the most-used tabbed and carousel page applications is “Instagram”. In instagram you can navigate between pages via tabs and via a swipe gesture.

Creating a Carousel page

Here I am using Visual Studio 2017 for Windows OS with Xamarin workload installed in it. Now we are going to make a carousel page application --  follow the steps to make it.

First make a xamarin.forms project. If you are new to xamarin.forms then learn to create your first cross-platform mobile application using Visual Studio 2017 here.

Now right click on your portable project -> Add -> New item

Xamarin

Then go to Cross – Platform -> Forms Carousel Page Xaml (rename it) -> click Add

Xamarin

Now, you will see some code in your Carousel page.

XAML 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="CsharpCorner.CarouselPageExample"  
  5.              Title="CarouselPage">  
  6.   <ContentPage>  
  7.     <StackLayout>  
  8.       <Label Text="This is a CarouselPage, you can swipe left and right to browse through additional pages." Margin="5" />  
  9.       <Label Text="Red" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5" />  
  10.       <BoxView Color="Red" VerticalOptions="FillAndExpand" />  
  11.     </StackLayout>  
  12.   </ContentPage>  
  13.   <ContentPage>  
  14.     <StackLayout>  
  15.       <Label Text="Green" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5"  />  
  16.       <BoxView Color="Green" VerticalOptions="FillAndExpand" />  
  17.     </StackLayout>  
  18.   </ContentPage>  
  19.   <ContentPage>  
  20.     <StackLayout>  
  21.       <Label Text="Blue" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5"  />  
  22.       <BoxView Color="Blue" VerticalOptions="FillAndExpand" />  
  23.     </StackLayout>  
  24.   </ContentPage>  
  25. </CarouselPage>   

Now, discuss the scenarios which are used to make Carousel page. A Carousel page is a parent page that contains multiple pages in it. You can add child pages by adding content page in your parent Carousel page. You can code multiple content pages in your parent page. Or you can also use reference of other page as a child of Carousel page.

But in this example, all the code is done on a main carousel page by adding content pages.

Add reference of other page

  1. <pages:Anypagename/>  

This line used is used to add ra eference of other page. Now this page will act as one of the page in your carousel page application. But in this application we are not using any other page reference but all the code is done in main page.

Add Content Page

And the code above is,

  1. <ContentPage>  
  2.   <StackLayout>  
  3.     <Label Text="This is a CarouselPage, you can swipe left and right to browse through additional pages." Margin="5" />  
  4.     <Label Text="Red" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5" />  
  5.     <BoxView Color="Red" VerticalOptions="FillAndExpand" />  
  6.   </StackLayout>  
  7. </ContentPage>   

This code will add a content page in your application but this is not the reference of the other page. This code will add another content page. You can add multiple content pages in your carousel page.

You can simply add content page by a single <ContentPage> tag.

Let’s run the application and see how your carousel page looks.

Output on android and windows desktop

Xamarin

So, this is the final output of our Carousel page application. Now you can navigate from one page to another by a swipe gesture and in windows desktop application navigation button is present, which is used for navigation between pages.

You can modify the page according to your desire and make cool applications with a carousel page.