Universal Windows Platform – Carousel Control

Carousel Control demonstrates how to create a Control to display items in a moving Carousel

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.


Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…


Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.


The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…


Step 6

From the Add New Item window select Visual C#, then XAML from Installed then select User Control from the list, then type in the Name as Carousel.xaml before selecting Add to add the file to the Project


Step 7

Once in the Design View for Carousel.xaml between the Grid and /Grid elements the following should be entered:

<Canvas Name="Display" HorizontalAlignment="Center" VerticalAlignment="Center" Loaded="Display_Loaded"/>

In the Carousel.xaml a Canvas has been defined, this will be where the Carousel itself will appear within.

Step 8

From the Menu choose View and then Code


Step 9

Once in the Code View, below the end of public Clock() { … } the following Code should be entered:

private Windows.UI.Xaml.Media.Animation.Storyboard _animation =
	new Windows.UI.Xaml.Media.Animation.Storyboard();
private List<Windows.UI.Xaml.Media.Imaging.BitmapImage> _list =
	new List<Windows.UI.Xaml.Media.Imaging.BitmapImage>();

private Point _point;
private Point _radius = new Point { X = -20, Y = 200 };
private double _speed = 0.0125;
private double _perspective = 55;
private double _distance;

private void Layout(ref Canvas display)
	for (int index = 0; index < _list.Count(); index++)
		_distance = 1 / (1 - (_point.X / _perspective));
		Image item = new Image
			Width = 150,
			Source = _list[index],
			Tag = index * ((Math.PI * 2) / _list.Count),
			RenderTransform = new ScaleTransform()
		_point.X = Math.Cos((double)item.Tag) * _radius.X;
		_point.Y = Math.Sin((double)item.Tag) * _radius.Y;
		Canvas.SetLeft(item, _point.X - (item.Width - _perspective));
		Canvas.SetTop(item, _point.Y);
		item.Opacity = ((ScaleTransform)item.RenderTransform).ScaleX =
			((ScaleTransform)item.RenderTransform).ScaleY = _distance;

private void Rotate()
	foreach (Image item in Display.Children)
		double angle = (double)item.Tag;
		angle -= _speed;
		item.Tag = angle;
		_point.X = Math.Cos(angle) * _radius.X;
		_point.Y = Math.Sin(angle) * _radius.Y;
		Canvas.SetLeft(item, _point.X - (item.Width - _perspective));
		Canvas.SetTop(item, _point.Y);
		if (_radius.X >= 0)
			_distance = 1 * (1 - (_point.X / _perspective));
			Canvas.SetZIndex(item, -(int)(_point.X));
			_distance = 1 / (1 - (_point.X / _perspective));
			Canvas.SetZIndex(item, (int)(_point.X));
		item.Opacity = ((ScaleTransform)item.RenderTransform).ScaleX =
			((ScaleTransform)item.RenderTransform).ScaleY = _distance;

public void Add(Windows.UI.Xaml.Media.Imaging.BitmapImage image)
	Layout(ref Display);

public void RemoveLast()
	if (_list.Any())
		Layout(ref Display);

public void New()
	Layout(ref Display);

private void Display_Loaded(object sender, RoutedEventArgs e)
	_animation.Completed += (object s, object obj) =>

Below the Clock() Method are the Members that make up the various elements of the Carousel, there is a Storyboard that will represent the cycling through the Carousel and the List of BitmapImage to represent the items in the Carousel. There are Members to define the speed, perspective and position of the middle of the Carousel, the Layout Method is used to create the look-and-feel of the Carousel and Rotate will move items around the Carousel. Add adds a BitmapImage to the set of items in the Carousel, RemoveLast will remove the last BitmapImage from the Carousel and New will clear all items from the List of BitmapImage. Display_Loaded is used to configure and initialise the Carousel.

Step 10

Once done select from the Menu, Build, then Build Solution


Step 11

In the Solution Explorer select MainPage.xaml


Step 12

From the Menu choose View and then Designer


Step 13

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	<TextBox Grid.Row="0" Name="Value" InputScope="Url" Margin="20,20,20,20" KeyDown="Go_KeyDown"/>
	<local:Carousel Width="400" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="Display"/>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Icon="Remove" Label="Remove" Click="Remove_Click"/>

The first block of XAML represents a Grid where the first row is a TextBox to enter the Url of an Image and the second row is where the Carousel Control itself will appear. The second block of XAML is a CommandBar with a AppBarButton which will perform the Remove action.

Step 14

From the Menu choose View and then Code


Step 15

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

private void Go_KeyDown(object sender, KeyRoutedEventArgs e)
	if (e.Key == Windows.System.VirtualKey.Enter)
		Display.Add(new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(Value.Text)));

private void New_Click(object sender, RoutedEventArgs e)

private void Remove_Click(object sender, RoutedEventArgs e)

Below the MainPage() Method there is a Go_KeyDown Method which will be used to add images to the Carousel, the New_Click Method is used to clear the Carousel and the Remove_Click is used to remove items from the Carousel

Step 16

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application


Step 17

After the Application has started running you can then type in the URL of any image e.g. https://dummyimage.com/300×200/000000/ffffff&text=Hello+World then press or tap enter to and add this to the Carousel you can add multiple images to increase the items shown in the Carousel


Step 18

To Exit the Application select the Close button in the top right of the Application


Creative Commons License

2 thoughts on “Universal Windows Platform – Carousel Control

  1. In step 9 you say Below the end of public Clock() {…} that’s a typo right? Is it supposed to say “public Carousel()”, or are we supposed to be adding all of this stuff into the same project as the one previous? Each tutorial starts with creating a new project… Otherwise I really appreciate these tutorials


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s