WPF Amazing Animation Library: XamlFlair

WPF Amazing Animation Library: XamlFlair

The goal of the XamlFlair library is to simplify the implementation of common animations and allow developers to easily add single or combined animation sets using a few lines of Xaml.

Last updated 7/3/2023 1:33 PM
沙漠尽头的狼
9 min read
Category
WPF
Topic
WPF Open Source Project WPF UI Design
Tags
.NET WPF WPF Open Source Project Open Source Animation Library

Original link: https://github.com/XamlFlair/XamlFlair

Translation: Desert End Wolf, Linluz (This article is not a full translation, it is recommended to read the original text for more details)

XamlFlair

The goal of the XamlFlair library is to simplify the implementation of common animations and allow developers to easily add single or combined animation sets using a few lines of Xaml.

Sample App

Showcase

Sekuence Puzzle Game| :-------------------------------------------: Sekuence |

Support the Author

If you'd like to support my work with a coffee, you can do so here: Buy Me a Coffee. Your support motivates me to continue spending time on this project and to keep maintaining and updating it with new features. Thanks in advance!

Table of Contents

Install from NuGet

Platform Package NuGet
UWP XamlFlair.UWP
WPF XamlFlair.WPF
Uno XamlFlair.Uno

Use the following command to download XamlFlair from the Package Manager Console:

UWP:

Install-Package XamlFlair.UWP

Your application must target Windows 10 version 1809 (build 17763) or higher.

WPF:

Install-Package XamlFlair.WPF

Uno:

Install-Package XamlFlair.Uno

Your UWP application must target at least Windows 10 version 1809 (build 18362).

Features Overview

Feature UWP WPF UWP (Uno) iOS (Uno) Android (Uno) Wasm (Uno) Experimental
Animation System Composition Storyboard Storyboard Storyboard Storyboard Storyboard
Transform Type N/A TransformGroup CompositeTransform CompositeTransform CompositeTransform CompositeTransform
DefaultAnimations.xaml - - - - -
TransformOn - - - - -
Compound Animations
Relative Translations
Repeating Animations
Events & Bindings
Primary/Secondary Completion Commands
StartWith
AllowOpacityReset - - - - -
ClipToBounds N/A
Animated Lists
Blur Effect - - - -
Saturation Effect - - - - -
Tint Effect - - - - -
Color Animations -
Perspective Rotations (Swivel) - - - - -
Debugging Animations -

Basic Concepts

The basic concept of XamlFlair is based on From and To animations. Any UI element composed of From animations will start with one or more arbitrary values and complete using the default values of the corresponding properties. Any UI element composed of To animations will start from its current state and be set to one or more arbitrary values.

Example of a From animation (a UI element moving to Translation(0)):

From Animation

Example of a To animation (a UI element sliding out from its current state):

To Animation

Note: It is important to note that there is an exception to this rule for color animations, which is explained in the "Base Animation Types" section.

Usage

First, add the following XAML namespace reference:

UWP and Uno:

xmlns:xf="using:XamlFlair"

WPF:

xmlns:xf="clr-namespace:XamlFlair;assembly=XamlFlair.WPF"

Add the attached property to any FrameworkElement that needs animation:

<Border xf:Animations.Primary="{StaticResource FadeIn}" />

Note: If the FrameworkElement has a CompositeTransform defined in XAML, it will be changed during the animation.

Note: The use of StaticResource refers to a globally common animation, which will be discussed in the next section.

Base Animation Types

Fade

Fade Animation

Warning: Be careful when setting FadeTo animations, because if the Visibility is Visible, the element will remain in the visual tree. In some cases, you may need to manage IsHitTestVisible manually to allow users to click the element.

Translate

Translate Animation

Scale

Scale Animation

Rotate

Rotate Animation

Blur (UWP and WPF only)

Blur Animation

Saturate (UWP only)

Saturate Animation

Tint (UWP only)

Tint Animation

Color (WPF and Uno only)

Color Animation

Note: It is important to note that when using From animations for color animations, the color will be set from the specified value to its current state, not the default value.

Swivel (UWP only)

Swivel Animation

Note: Please read the Perspective Rotations (UWP Only) section for more details.

Below is a list of some notable default values when using XamlFlair:

  • Kind: FadeTo
  • Duration (milliseconds): 500
  • Easing: Cubic
  • Easing Mode: EaseOut
  • TransformCenterPoint: (0.5, 0.5)
  • Event: Loaded
  • InterElementDelay (milliseconds): 25 (List controls only)
  • TransformOn: Render (WPF only)
  • Saturation: 0.5 (UWP only)
  • Tint: Transparent (UWP only)

Color Animations (WPF and Uno Only)

When using color animations, it is important to note that they are slightly different from other base animation types. When using ColorTo and ColorFrom, the following must be done:

  • Only the following properties can be animated: Control.Background, Control.Foreground, Control.BorderBrush, Border.Background, Border.BorderBrush, TextBlock.Foreground, Shape.Fill, Shape.Stroke
  • Ensure a brush is set on the corresponding property to be animated
  • The target property must also be specified using ColorOn

The following example will animate the Fill property of a Rectangle from RoyalBlue to DarkGreen:

<xf:AnimationSettings
  x:Key="SampleColorAnimation"
  Kind="ColorTo"
  Color="DarkGreen"
  ColorOn="Fill"
/>

<Rectangle
  Fill="RoyalBlue"
  xf:Animations.Primary="{StaticResource SampleColorAnimation}"
/>

Overriding the Global Default Values

If you need to globally change one of the default animation values (for example, change the default Duration to 750 instead of 500), you can call the OverrideDefaultSettings function in your application's initialization code. The following example changes the default values for Duration and Easing:

XamlFlair.Animations.OverrideDefaultSettings(
    duration: 750,
    easing: EasingType.Quadratic);

Thus, in the example code above, every animation will run for 750ms with a quadratic easing.

Using a ResourceDictionary for Base Settings

All common animations should be placed in a global ResourceDictionary (e.g., Animations.xaml) and referenced in the application when needed. The goal is to consolidate all animations into a single file with meaningful names so that any developer can understand exactly what animation will be applied to the FrameworkElement. Here's a small example:

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:xf="using:XamlFlair"
>
  <x:Double x:Key="PositiveOffset">50</x:Double>
  <x:Double x:Key="NegativeOffset">-50</x:Double>
  <x:Double x:Key="SmallScaleFactor">0.75</x:Double>
  <x:Double x:Key="LargeScaleFactor">1.25</x:Double>

  <xf:AnimationSettings x:Key="FadeIn" Kind="FadeFrom" Opacity="0" />

  <xf:AnimationSettings x:Key="FadeOut" Kind="FadeTo" Opacity="0" />

  <!-- Scale to a larger value -->
  <xf:AnimationSettings
    x:Key="Expand"
    Kind="ScaleXTo,ScaleYTo"
    ScaleX="{StaticResource LargeScaleFactor}"
    ScaleY="{StaticResource LargeScaleFactor}"
  />

  <!-- Scale from a larger value -->
  <xf:AnimationSettings
    x:Key="Contract"
    Kind="ScaleXFrom,ScaleYFrom"
    ScaleX="{StaticResource LargeScaleFactor}"
    ScaleY="{StaticResource LargeScaleFactor}"
  />
  . . .
</ResourceDictionary>

To use this pre-configured set of AnimationSettings in your application, follow these steps:

  1. Right-click on your project in Solution Explorer, then click Add > New Item...
  2. Select Resource Dictionary and name it Animations.xaml
  3. Add the following to App.xaml:
<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Animations.xaml" />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>
  1. In Animations.xaml, copy and paste the content from the appropriate link below:

Your application now has a set of common animations ready to use.

Default Animations (WPF Only)

In addition to creating a custom ResourceDictionary with AnimationSettings, XamlFlair also provides some default animations.

To reference these default animations in your application, follow these steps in App.xaml:

  1. Add the XamlFlair.WPF namespace at the top:
xmlns:xf="clr-namespace:XamlFlair;assembly=XamlFlair.WPF"
  1. Update the Application Resources:
<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <xf:XamlFlairResources />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

Your application now has a set of global default animations ready to use.

If Visual Studio IntelliSense is not working when using <xf:XamlFlairResources />, you may need to try the following:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary
        Source="pack://application:,,,/XamlFlair.WPF;component/DefaultAnimations.xaml"
      />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

TransformOn Property (WPF Only)

The RenderTransform can be animated using the TransformOn property. Available options are Render and Layout. When unspecified, it defaults to Render. Here's an example of both options:

TransformOn Animation

Note: It is very important to note that WPF's LayoutTransform does not support any TranslateTransform, so translate animations will never take effect. You can learn more in the remarks section here.

The original readme.md is too long, and the translation is getting lengthy. If you are interested, please refer to the original text. Finally, here's an image:

Keep Exploring

Related Reading

More Articles
Same category / Same tag 9/13/2025

Migration Series from WPF to Avalonia: Why I Must Migrate My WPF Application to Avalonia

In the past few years, our host computer software has mainly been developed using WPF and WinForm . These technologies work well on the Windows platform and have accompanied us from small-scale trial production to the current stage of large-scale delivery. However, with business development and changes in customer requirements, the single Windows technology stack has gradually become a hurdle we must overcome.

Continue Reading
Same category / Same tag 1/26/2025

Implementing Internationalization in WPF Using Custom XML Files

This article details the method of implementing internationalization in WPF applications using custom XML files, including installing the necessary NuGet packages, dynamically retrieving the language list, dynamically switching languages, using translated strings in code and XAML interfaces, and provides a source code link to help developers easily achieve internationalization in WPF applications.

Continue Reading