Windows 10 Universal Windows Platform – Carousel Control

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

10-home

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

10-file-new-project

Step 3

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

10-new-project

Step 4

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

10-project-add-new-item

Step 5

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

10-add-new-item-carouselcontrol

Step 6

The Design View will be displayed along with the XAML View and in this below <Grid> enter the following XAML:

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

It should appear as such:

10-usercontrol-xaml-carouselcontrol

Step 7

Select from the Menu, View then Code

10-code

Step 8

Once in the Code View for the User Control the following should be entered below the public Carousel() { … } method:

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++)
	{
		Image item = new Image();
		item.Width = 150;
		item.Source = list[index];
		item.Tag = index * ((Math.PI * 2) / list.Count);
		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);
		distance = 1 / (1 - (point.X / perspective));
		item.RenderTransform = new ScaleTransform();
		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();
}

private void init()
{
	animation.Completed += (object sender, object e) =>
	{
		rotate();
	};
	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);
}

Also inside the public Carousel() { … } method below this.InitializeComponent(); the following should be entered:

init();

It should then appear as such:

10-usercontrol-code-carouselcontrol

Step 9

From the Solution Explorer select MainPage.xaml

10-mainpage

Step 10

Select from the Menu, View then Designer

10-designer

Step 11

The Design View will be displayed along with the XAML View and in this below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> 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>

It should appear as such:

10-xaml-carouselcontrol

Step 12

Select from the Menu, View then Code

10-code

Step 13

Once in the Code View below the public MainPage() { … } the following 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();
}

It should then appear as such:

10-code-carouselcontrol

Step 14

That completes the Windows Universal Application so Save the Project then select the Debug and Simulator option to run the Application

10-simulator

Step 15

Once the Simulator has started the Application should then appear

10-simulator-run-carouselcontrol

Step 16

After the Application has started running you can then type in the URL of any image e.g. http://dummyimage.com/300×200&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

10-simulator-ran-carouselcontrol

Step 17

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Step 18

Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application

10-emulator

Step 19

Once the Emulator has started the Application should then appear

10-emulator-run-carouselcontrol

Step 20

After the Application has started running you can then type in the URL of any image e.g. http://dummyimage.com/300×200&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

10-emulator-ran-carouselcontrol

Step 21

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Creative Commons License

Advertisements

One thought on “Windows 10 Universal Windows Platform – Carousel Control

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 )

Google+ photo

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

Connecting to %s