Connected Animation demonstrates how to use a Connected Animation which is part of the Fluent Design System in Windows 10.
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, version 1803 (10.0; Build 17134) which is the April 2018 Update 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 Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System.Linq; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Shapes; public static class Library { private const string animate_back = "AnimateBack"; private const string animate_next = "AnimateNext"; public static string Current { get; set; } public static void Back(ref ListView listview) { Rectangle rectangle = (Rectangle)listview.Items .SingleOrDefault(f => ((Rectangle)f).Tag.Equals(Current)); Windows.UI.Xaml.Media.Animation.ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().GetAnimation(animate_back); animation?.TryStart(rectangle); } public static Brush Next(ref object selected) { Rectangle rectangle = (Rectangle)selected; Current = (string)rectangle.Tag; Windows.UI.Xaml.Media.Animation.ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().PrepareToAnimate(animate_next, rectangle); return rectangle.Fill; } public static void From(ref Rectangle from) { Windows.UI.Xaml.Media.Animation.ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().PrepareToAnimate(animate_back, from); } public static void Loaded(ref Rectangle rectangle) { Windows.UI.Xaml.Media.Animation.ConnectedAnimation animation = ConnectedAnimationService.GetForCurrentView().GetAnimation(animate_next); rectangle.Opacity = 1; animation?.TryStart(rectangle); } }
In the Library Class there is a const of string, there’s a Back Method that takes a ListView as a Parameter and gets a Rectangle from the ListView and then gets the Windows.UI.Xaml.Media.Animation.ConnectedAnimation and calls the TryStart Method on it.
There is a Next Method which takes an object as a Parameter which will be a Rectangle and then gets the Windows.UI.Xaml.Media.Animation.ConnectedAnimation for it and calls the PrepareToAnimate Method of the ConnectedAnimationService.GetForCurrentView and there is a From Method that calls the ConnectedAnimationService.GetForCurrentView Method of PrepareToAnimate.
The Loaded method takes a Rectangle as a Parameter and this calls the GetAnimation Method of ConnectedAnimationService.GetForCurrentView and will set the Opacity to 1 and calls the TryStart Method.
Step 8
Once done select from the Menu, Project, then Add New Item…
Step 9
From the Add New Item window select Visual C#, then XAML from Installed then select Blank Page from the list, then type in the Name as DetailPage.xaml before selecting Add to add the file to the Project
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Rectangle Margin="50" Name="Target" Opacity="0" Loaded="Target_Loaded"/> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Icon="Back" Label="Back" Click="Back_Click"/> </CommandBar>
The first block of XAML is a Rectangle with a Rectangle Control and the second block of XAML is a CommandBar with an AppBarButton for Back
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public DetailPage() { … } the following Code should be entered:
protected override void OnNavigatedTo(NavigationEventArgs e) { Target.Fill = (SolidColorBrush)e.Parameter; } protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { if (e.NavigationMode == NavigationMode.Back) Library.From(ref Target); base.OnNavigatingFrom(e); } private void Target_Loaded(object sender, RoutedEventArgs e) { Library.Loaded(ref Target); } private void Back_Click(object sender, RoutedEventArgs e) { this.Frame.GoBack(); }
The there is an OnNavigatedTo Event handler that will set the Fill property of the Rectangle, OnNavigatingFrom which will call the From Method in the Library Class and Back_Click which will call GoBack to navigate to the previous XAML Page, MainPage.xaml.
Step 13
In the Solution Explorer select MainPage.xaml
Step 14
From the Menu choose View and then Designer
Step 15
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<ListView Name="Display" Margin="50"> <Rectangle Margin="10" Width="64" Height="64" Tag="Black" Fill="Black" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Gray" Fill="Gray" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Red" Fill="Red" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Orange" Fill="Orange" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Yellow" Fill="Yellow" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Green" Fill="Green" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Cyan" Fill="Cyan" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Blue" Fill="Blue" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Magenta" Fill="Magenta" Tapped="Rectangle_Tapped"/> <Rectangle Margin="10" Width="64" Height="64" Tag="Purple" Fill="Purple" Tapped="Rectangle_Tapped"/> </ListView>
The main block of XAML is a ListView Control which contains Rectangle Controls with their Tapped Event Handler set.
Step 16
From the Menu choose View and then Code
Step 17
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
protected override void OnNavigatedTo(NavigationEventArgs e) { if (e.NavigationMode == NavigationMode.Back) Library.Back(ref Display); base.OnNavigatedTo(e); } private void Rectangle_Tapped(object sender, TappedRoutedEventArgs e) { this.Frame.Navigate(typeof(DetailPage), Library.Next(ref sender)); }
There is an OnNavigatedTo Event Handler which will call the Back Method from the Library Class and Rectangle_Tapped which will call the Navigate Method of the Page Frame and pass the DetailPage and the result of the Next Method in the Library Class.
Step 18
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 19
Once the Application has started running you can tap on any of the Rectangle Controls, this will Navigate to the DetailsPage to show a larger version of a Rectangle with the same Fill but will use a Connected Animation to transition to and from that page.
Step 20
To Exit the Application select the Close button in the top right of the Application
This example shows how it’s possible to use a Connected Animation to transition between two pages for example when you have a list of Photographs on one view you can select it to show a larger version and have a Connected Animation transition between them.