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.

vs2017

Step 2

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

vs2017-file-new-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
vs2017-new-project-window

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.

vs2017-target-platform

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…

vs2017-project-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

vs2017-add-new-item-carousel-control

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

vs2017-view-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)
{
	display.Children.Clear();
	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;
		display.Children.Add(item);
	}
}

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));
		}
		else
		{
			_distance = 1 / (1 - (_point.X / _perspective));
			Canvas.SetZIndex(item, (int)(_point.X));
		}
		item.Opacity = ((ScaleTransform)item.RenderTransform).ScaleX =
			((ScaleTransform)item.RenderTransform).ScaleY = _distance;
	}
	_animation.Begin();
}

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

public void RemoveLast()
{
	if (_list.Any())
	{
		_list.Remove(_list.Last());
		Layout(ref Display);
	}
}

public void New()
{
	_list.Clear();
	Layout(ref Display);
}

private void Display_Loaded(object sender, RoutedEventArgs e)
{
	_animation.Completed += (object s, object obj) =>
	{
		Rotate();
	};
	_animation.Begin();
}

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

vs2017-build-build-solution

Step 11

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 12

From the Menu choose View and then Designer

vs2017-view-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:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<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"/>
</Grid>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Icon="Remove" Label="Remove" Click="Remove_Click"/>
</CommandBar>

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

vs2017-view-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)
{
	Display.New();
}

private void Remove_Click(object sender, RoutedEventArgs e)
{
	Display.RemoveLast();
}

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

vs2017-local-machine

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

ran-carousel-control

Step 18

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

vs2017-close

Creative Commons License

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 )

Google+ photo

You are commenting using your Google+ 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