Carousel Control In UWP

The Carousel control inherits from ItemsControl, representing a nice and smooth carousel. Carousel Control has a lot of properties for a flexible layouting. This control works fine with mouse, touch, and keyboard as well. Carousel Control is from UWP Community Toolkit. The UWP Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer tasks building UWP apps for Windows 10.

Reading this article, you can learn how to use Carousel Control in Universal Windows Apps development with XAML and Visual C#.

The following important tools are required for developing UWP app.

  1. Windows 10 (Recommended)
  2. Microsoft Visual Studio 2017

Step 1

Open Visual Studio 2017 and go to Start -> New Project-> select Windows Universal (under Visual C#)-> Blank App (Universal Windows) -> give it a suitable name (ex- UWPCarousel)->OK.

 

After choosing the target and minimum platform version that your Windows Universal application will support, the Project creates App.xaml and MainPage.xaml.

 

Step 2

First, right-click the project and select "Manage NuGet Packages". Then, update to the latest version of Microsoft.NETCore.UniversalWindowsPlatform.

For adding UWPToolKit Control, please refer the below link.

http://www.c-sharpcorner.com/article/how-to-add-uwp-toolkit-controls-in-universal-application-development-with-xaml-a/

Step 3

Add the following controls in design window for Carousal control view. Add a TextBlock Control and change its name and text property.

Add the Carousel Control and change the Name, InvertPositive, ItemDepth, ItemMargin, ItemRotationX, ItemRotationY, ItemRotationZ,Orientation, ItemTemplate, SelectedIndex, Margin, EasingFunction Properties.

  1. <Controls:Carousel x:Name="CaroTest" InvertPositive="True" ItemDepth="300" ItemMargin="19" ItemRotationX="43" ItemRotationY="65" ItemRotationZ="42" Orientation="Horizontal" ItemTemplate="{StaticResource photos}" SelectedIndex="2" Margin="0,39,0,263">  
  2.     <Controls:Carousel.EasingFunction>  
  3.         <CubicEase EasingMode="EaseOut" /> </Controls:Carousel.EasingFunction>  
  4. </Controls:Carousel>  
 

Step 4

Create an "Images" folder and add images for Carousel control.

 

 Step 5

Add the XAML code for image resource for Carousel.

  1. <Page.Resources>  
  2.     <DataTemplate x:Key="photos">  
  3.         <Grid Background="White" BorderBrush="Black" BorderThickness="1">  
  4.             <Image Source="{Binding CItem}" Width="200" Height="200" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid>  
  5.     </DataTemplate>  
  6. </Page.Resources>  
Step 6

Add the following namespace and code in MainPage.xaml.cs.

  1. using System.Collections.ObjectModel;  
  2. public class CaroItem {  
  3.     public string CItem {  
  4.         get;  
  5.         set;  
  6.     }  
  7. }  
  8. public sealed partial class MainPage: Page {  
  9.     ObservableCollection < CaroItem > items = new ObservableCollection < CaroItem > ();  
  10.     public MainPage() {  
  11.         this.InitializeComponent();  
  12.         items.Add(new CaroItem() {  
  13.             CItem = string.Format("ms-appx:///Images//01.jpg")  
  14.         });  
  15.         items.Add(new CaroItem() {  
  16.             CItem = string.Format("ms-appx:///Images//02.jpg")  
  17.         });  
  18.         items.Add(new CaroItem() {  
  19.             CItem = string.Format("ms-appx:///Images//03.jpg")  
  20.         });  
  21.         items.Add(new CaroItem() {  
  22.             CItem = string.Format("ms-appx:///Images//04.jpg")  
  23.         });  
  24.         items.Add(new CaroItem() {  
  25.             CItem = string.Format("ms-appx:///Images//05.jpg")  
  26.         });  
  27.         items.Add(new CaroItem() {  
  28.             CItem = string.Format("ms-appx:///Images//06.jpg")  
  29.         });  
  30.         items.Add(new CaroItem() {  
  31.             CItem = string.Format("ms-appx:///Images//07.jpg")  
  32.         });  
  33.         items.Add(new CaroItem() {  
  34.             CItem = string.Format("ms-appx:///Images//08.jpg")  
  35.         });  
  36.         CaroTest.ItemsSource = items;  
  37.     }  
  38. }  

Note

Automatically, the following code will be generated in XAML code view while we are done in the design view.

  1. <Page x:Class="UWPCarousel.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPCarousel" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Controls="using:Microsoft.Toolkit.Uwp.UI.Controls" mc:Ignorable="d">  
  2.     <Page.Resources>  
  3.         <DataTemplate x:Key="photos">  
  4.             <Grid Background="White" BorderBrush="Black" BorderThickness="1">  
  5.                 <Image Source="{Binding CItem}" Width="200" Height="200" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid>  
  6.         </DataTemplate>  
  7.     </Page.Resources>  
  8.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  9.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="88,43,0,0" TextWrapping="Wrap" Text="Carousel Control in UWP" VerticalAlignment="Top" FontWeight="Bold" />  
  10.         <Border Margin="0,32,0,-32">  
  11.             <Controls:Carousel x:Name="CaroTest" InvertPositive="True" ItemDepth="300" ItemMargin="19" ItemRotationX="43" ItemRotationY="65" ItemRotationZ="42" Orientation="Horizontal" ItemTemplate="{StaticResource photos}" SelectedIndex="2" Margin="0,39,0,263">  
  12.                 <Controls:Carousel.EasingFunction>  
  13.                     <CubicEase EasingMode="EaseOut" /> </Controls:Carousel.EasingFunction>  
  14.             </Controls:Carousel>  
  15.         </Border>  
  16.     </Grid>  
  17. </Page>  
Step 7

Deploy your app on a local machine. The output of the UWPCarousel is shown below.


After moving to the next image.

 

Summary

Now, you have successfully tested Carousel Control in XAML and UWP environment using Visual Studio 2017.


Similar Articles