Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting the Connected Animations sample #180

Merged
merged 11 commits into from
Aug 29, 2023
32 changes: 32 additions & 0 deletions components/Animations/samples/Animations.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,36 @@

<!-- Sets this up as a toolkit component's sample project -->
<Import Project="$(ToolingDirectory)\ToolkitComponent.SampleProject.props" />
<ItemGroup>
<None Remove="Assets\BigFourSummerHeat2.jpg" />
<None Remove="Assets\BisonBadlandsChillin2.jpg" />
<None Remove="Assets\ColumbiaRiverGorge.jpg" />
<None Remove="Assets\GrandTetons.jpg" />
<None Remove="Assets\MitchellButtes.jpg" />
<None Remove="Assets\OregonWineryNamaste.jpg" />
<None Remove="Assets\RunningDogPacificCity.jpg" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\BigFourSummerHeat2.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Assets\BisonBadlandsChillin2.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Assets\ColumbiaRiverGorge.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Assets\GrandTetons.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Assets\MitchellButtes.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Assets\OregonWineryNamaste.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Assets\RunningDogPacificCity.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
10 changes: 5 additions & 5 deletions components/Animations/samples/AnimationsImplicitSample.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="AnimationsExperiment.Samples.AnimationsImplicitSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -8,10 +8,9 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="CommunityToolkit.WinUI"
mc:Ignorable="d">
<Grid>
<Grid Height="480">
<Grid.RowDefinitions>
<RowDefinition Height="*"
MinHeight="420" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Canvas>
Expand All @@ -20,7 +19,8 @@
Canvas.Top="100"
Width="100"
Height="100"
Background="{ThemeResource AccentFillColorDefaultBrush}">
Background="{ThemeResource AccentFillColorDefaultBrush}"
CornerRadius="{StaticResource ControlCornerRadius}">

<animations:Implicit.ShowAnimations>
<animations:TranslationAnimation From="0, -200, 0"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
194 changes: 194 additions & 0 deletions components/Animations/samples/ConnectedAnimations.md
niels9001 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
---
title: Connected Animations
author: nmetulev
description: The Connected Animation XAML Attached Properties enable connected animations to be defined in your XAML code
keywords: Animations, Effects, Layout, Composition, animationset, animation, coordinated animations
dev_langs:
- csharp
category: Animations
subcategory: Layout
discussion-id: 0
issue-id: 0
icon: Assets/ConnectedAnimations.png
---


# Connected Animations XAML Attached Properties

[Connected animations](/windows/uwp/style/connected-animation) let you create a dynamic and compelling navigation experience by animating the transition of an element between two different views.

The Connected Animations XAML Attached Properties enable connected animations to be defined directly in your XAML code by simply adding a Key to the element that should animate. There are also attached properties to enable coordinated animations and animations in lists and grids.

> [!Sample ConnectedAnimationsSample]

## Syntax

```xaml
<Page ...
xmlns:animations="using:CommunityToolkit.WinUI.Animations">

<Border x:Name="Element" animations:Connected.Key="item"></Border>

<TextBlock animations:Connected.AnchorElement="{x:Bind Element}" Text="Hello World"/>

<GridView animations:Connected.ListItemElementName="ItemThumbnail"
animations:Connected.ListItemKey="listItem">
<GridView.ItemTemplate>
<DataTemplate>
<Image x:Name="ItemThumbnail" Height="200" Width="200"></Image>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Page>
```

## XAML Attached Properties

### Connected.Key

Registers element with the [ConnectedAnimationsService](/uwp/api/Windows.UI.Xaml.Media.Animation.ConnectedAnimation). For the animation to work, the same key must be registered on two different pages when navigating

### Connected.AnchorElement

To enable [coordinated animations](/windows/uwp/style/connected-animation#coordinated-animation), use the AnchorElement attached property on the element that should appear alongside the connected animation element by specifying the connected animation element

### Connected.ListItemKey

Registers a ListView/GridView for connected animations. When navigating from/to a page that is using this property, the connected animation will use the item passed as a **parameter** in the page navigation to select the item in the list that should animated. See *Select List Item to be animated* section below for more details. The *Connected.ListItemElementName* attached property must also be set for the animation to be registered

### Connected.ListItemElementName

Specifies what named element in the DataTemplate of an item should animate. The Connected.ListItemKey attached property must also be set for the animation to be registered.

## Registering elements in code behind

In cases where an element has not loaded before the navigation completes, the attached properties are not able to access the element properties to register it for the connected animation. In those case, you can register the element through code behind inside the OnNavigatedTo method. The following extension methods are available:

### RegisterElementForConnectedAnimation(this Page page, string key, UIElement element, IEnumerable\<UIElement> anchors = null)

Registers a UIElement with the ConnectedAnimations service to run automatically on page navigation

### UnregisterElementForConnectedAnimation(this Page page, string key)

Unregisters a UIElement from the ConnectedAnimations service

### AttachAnchorElementForConnectedAnimation(this Page page, UIElement element, UIElement anchor)

Add an anchor element to animate alongside the main element

### RemoveAnchoredElementForConnectedAnimation(this Page page, UIElement element, UIElement anchor)

Remove an anchor element from animating alongside the main element

### RegisterListItemForConnectedAnimation(this Page page, ListViewBase listViewBase, string key, string elementName)

Registers an element (part of a DataTemplate in a list control) with the ConnectedAnimations service to run automatically on page navigation

### UnregisterListItemForConnectedAnimation(this Page page, ListViewBase listViewBase, string key)

Unregisters an element (part of a DataTemplate in a list control) from the ConnectedAnimations service

## Select List Item to be animated

The helper uses the page navigation parameter to decide which list item will be animated during the page navigation. However, in some cases the parameter passed during page navigation is not part of the list. For example, you might be only passing the id of an item as a navigation parameter and not the item itself.

In those cases, you can use the **SetListDataItemForNextConnectedAnnimation** extension method before page navigation to specify which item should be animated.

```csharp
// dataItemToAnimate is an object in the ListViewBase.ItemsSource collection
Frame.SetListDataItemForNextConnectedAnnimation(dataItemToAnimate);
Frame.Navigate(typeof(DetailsPage), dataItemToAnimate.Id);
```

```vb
' dataItemToAnimate is an object in the ListViewBase.ItemsSource collection
Frame.SetListDataItemForNextConnectedAnnimation(dataItemToAnimate)
Frame.Navigate(GetType(DetailsPage), dataItemToAnimate.Id)
```

This method is also helpful when navigating back to an item different from the item it was navigated from.

```csharp
Frame.SetListDataItemForNextConnectedAnnimation(dataItemToAnimate);
Frame.GoBack();
```

```vb
Frame.SetListDataItemForNextConnectedAnnimation(dataItemToAnimate)
Frame.GoBack()
```

## Examples

We can create the above connected animations.

### In first page

We need a set a key for the element to be connected with another element in a different page.

```xaml
<Grid>
<Border Height="100" Width="100" Background="Purple"
VerticalAlignment="Center" HorizontalAlignment="Center"
animations:Connected.Key="item" />
</Grid>
```

### In second page

We need to set the same key for the element to be connected with. Also, You can anchor another element to move along the connected animation path.

```xaml
<StackPanel Orientation="Horizontal">
<Border x:Name="HeroElement" Height="300" Width="300" Background="Purple"
animations:Connected.Key="item"/>

<StackPanel x:Name="HeroDetailsElement" Margin="20,0"
VerticalAlignment="Bottom" MaxWidth="500"
animations:Connected.AnchorElement="{x:Bind HeroElement}">
<TextBlock Text="Header" FontSize="50">Header</TextBlock>
<TextBlock TextWrapping="WrapWholeWords">Lorem ipsum ...</TextBlock>
</StackPanel>
</StackPanel>
```

In this page, we can also create a GridView which implements connected animation for its items. You need to set ListItemKey and ListItemElementName for specifying the UIElement to animate.

```xaml
<GridView x:Name="listView" Margin="0, 40, 0, 0" SelectionMode="None"
Grid.Row="1" ItemClick="ListView_ItemClick" IsItemClickEnabled="True"
animations:Connected.ListItemElementName="ItemThumbnail"
animations:Connected.ListItemKey="listItem">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:Item">
<StackPanel>
<Border x:Name="ItemThumbnail" Background="Purple" Height="200" Width="200"></Border>
<TextBlock Text="{x:Bind Title}"></TextBlock>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
```

### In third page

In this page, you just need to give the same key.

```xaml
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<StackPanel x:Name="HeroDetailsElement" Margin="20,0" VerticalAlignment="Bottom" MaxWidth="500"
animations:Connected.AnchorElement="{x:Bind ItemHeroElement}">
<TextBlock Text="{x:Bind item.Title}"
FontSize="50"/>
<TextBlock TextWrapping="WrapWholeWords">Lorem ipsum ...</TextBlock>
</StackPanel>

<Border x:Name="ItemHeroElement" Height="300" Width="300" Background="Purple"
animations:Connected.Key="listItem"/>
</StackPanel>

<TextBlock Margin="0,40" TextWrapping="WrapWholeWords">Lorem Ipsum ...</TextBlock>
</StackPanel>
```

27 changes: 27 additions & 0 deletions components/Animations/samples/ConnectedAnimations/FirstPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Page x:Class="AnimationsExperiment.Samples.ConnectedAnimations.FirstPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:animations="using:CommunityToolkit.WinUI.Animations"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel Margin="32"
Orientation="Vertical"
Spacing="32">

<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Top"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="This is the first page, Click or Tap the box to navigate to the next page" />

<Border Width="120"
Height="120"
HorizontalAlignment="Center"
VerticalAlignment="Center"
animations:Connected.Key="item"
Background="{ThemeResource AccentFillColorDefaultBrush}"
CornerRadius="{StaticResource ControlCornerRadius}"
Tapped="Border_Tapped" />
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace AnimationsExperiment.Samples.ConnectedAnimations;

public sealed partial class FirstPage : Page
{
public FirstPage()
{
this.InitializeComponent();
}

private void Border_Tapped(object sender, TappedRoutedEventArgs e)
{
Frame.Navigate(typeof(SecondPage), null, new SuppressNavigationTransitionInfo());
}
}
63 changes: 63 additions & 0 deletions components/Animations/samples/ConnectedAnimations/SecondPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<Page x:Class="AnimationsExperiment.Samples.ConnectedAnimations.SecondPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:animations="using:CommunityToolkit.WinUI.Animations"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:AnimationsExperiment.Samples.ConnectedAnimations"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Padding="32"
RowSpacing="36">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">

<Border x:Name="HeroElement"
Width="120"
Height="120"
animations:Connected.Key="item"
Background="{ThemeResource AccentFillColorDefaultBrush}"
CornerRadius="{StaticResource ControlCornerRadius}" />

<StackPanel x:Name="HeroDetailsElement"
MaxWidth="500"
Margin="24,0"
VerticalAlignment="Bottom"
animations:Connected.AnchorElement="{x:Bind HeroElement}">
<TextBlock Style="{StaticResource TitleTextBlockStyle}"
Text="Header" />
<TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce eleifend ex sit amet blandit lobortis. Curabitur ut diam fringilla, interdum massa sit amet, facilisis erat. Donec vulputate sed ex vel pellentesque. In sodales odio non felis interdum viverra. Morbi in mi mollis, ullamcorper nibh sit amet, sagittis ex. Maecenas dapibus commodo venenatis. Donec at egestas est."
TextWrapping="WrapWholeWords" />
</StackPanel>
</StackPanel>

<GridView x:Name="listView"
Grid.Row="1"
animations:Connected.ListItemElementName="ItemThumbnail"
animations:Connected.ListItemKey="listItem"
IsItemClickEnabled="True"
ItemClick="ListView_ItemClick"
SelectionMode="None">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:PhotoDataItem">
<Grid Width="120"
Height="120">
<Image x:Name="ItemThumbnail"
Source="{x:Bind Thumbnail}"
Stretch="UniformToFill" />
<Border Padding="8"
VerticalAlignment="Bottom"
Background="{ThemeResource ControlOnImageFillColorDefaultBrush}">
<TextBlock FontSize="14"
Text="{x:Bind Title}" />
</Border>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
</Page>
Loading
Loading