WPF's awesome animation library: XamlFlair

WPF's awesome animation library: XamlFlair

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

最后更新 7/3/2023 1:33 PM
沙漠尽头的狼
预计阅读 8 分钟
分类
WPF
专题
WPF Open Source Project WPF UI Design
标签
.NET WPF WPF Open Source Project open source animation library

Original link: https://www.example.com

Translation: Wolf at the end of the desert, Linluz (This article has not been translated in full, it is recommended to read the original to learn more)

XamlFlair

XamlFlair 库的目标是简化常见动画的实现,并允许开发人员使用几行 Xaml轻松地添加单个或组合的动画集。

案例程序

display

Sekuence 益智游戏| :-------------------------------------------: Sekuence |

support the author

如果你想用一些咖啡来支持我的工作,你可以在这里做:给我买杯咖啡。你的帮助让我有动力继续花时间在这个项目上,并继续维护和更新它的新功能。提前谢谢!

content

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 be no less than Windows 10 version 1809 (build 17763)

WPF:

Install-Package XamlFlair.WPF

Uno:

Install-Package XamlFlair.Uno

Your UWP application must be at least for Windows 10 version 1809 (Build 18362)

Feature Overview (Features Overview)

Feature UWP WPF UWP (Uno) iOS (Uno) Android (Uno) * * Wasm (Uno) Experimental **
Animation System combination storyboard storyboard storyboard storyboard storyboard
Transformation type N/A transformation group composite transformation composite transformation composite transformation composite transformation
DefaultAnimations.xaml - - - - -
TransformOn - - - - -
Compound Animations
Relative translations
Repeating Animations
Events & Bindings
Primary/secondary completion command
StartWith
AllowOpacityReset - - - - -
ClipToBounds N/A
Animated Lists
Blur Effect - - - -
Saturation effect - - - - -
Tint Effect - - - - -
Color Animations -
Perspective Rotations (Swivel) - - - - -
Debugging Animations -

Basic Concepts (Basic Concepts)

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

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

From动画

To animation example (UI element slid out of current state):

To动画

    • Note **: Note that for color animations, there is an exception to this rule, which is explained in the "Basic Animation Types" section.

use

First, you need to add the following Xaml namespace references:

UWP and Uno:

xmlns:xf="using:XamlFlair"

WPF:

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

给任何需要动画的 UI 元素FrameworkElement添加附加属性:

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

注意:如果FrameworkElement在 Xaml 中定义了CompositeTransform,则它将在动画过程中更改。

注意StaticResource的用法是引用全局通用动画,这将在下一节中讨论。

Basic Animation Types (Base Animation Types)

Fade in and out

淡入淡出动画

警告:设置FadeTo动画时要小心,因为如果VisibilityVisible,元素将保留在可视树中。在某些情况下,您可能需要手动管理IsHitTestVisible,以允许用户点击元素。

Mobile (Translate)

移动动画

Scale

缩放动画

Rotate

旋转动画

Blur (only supports UWP and WPF)

模糊动画

Saturation (UWP only)

饱和度动画

Tint (UWP only supports)

色调动画

Color (Color, only supports WPF and Uno)

色彩动画

注意:重要的是要注意,当使用From动画设置色彩动画时,颜色将从指定值设置为其当前状态,而不是默认值。

Swivel (UWP only)

旋轴动画

注意:请阅读Perspective Rotations (UWP Only)一节了解更多详细信息。

下面列出了使用 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)

使用色彩动画时需要注意,因为它们与其他基本类型动画略有不同。使用ColorToColorFrom时,必须执行以下操作:

  • 只能设置以下属性的动画: Control.Background, Control.Foreground, Control.BorderBrush, Border.Background, Border.BorderBrush, TextBlock.Foreground, Shape.Fill, Shape.Stroke
  • Make sure to set brush on the appropriate property you want to animate
  • 还必须使用ColorOn指定目标属性

以下示例将为 Rectangle 的Fill属性设置从 RoyalBlue 到 DarkGreen 的动画:

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

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

Overriding global defaults

如果需要全局更改默认动画值之一(例如,默认Duration为 750 而不是 500),则可以在应用程序的初始化代码中调用OverrideDefaultSettings函数。以下示例更改DurationEasing的默认值:

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

Therefore, in the example code above, each animation will run for 750 ms in a secondary moderated manner.

使用ResourceDictionary进行基本设置

所有常见动画都应该放在全局ResourceDictionary(例如:Animations.xaml)中,并在应用程序中需要时使用。目标是将所有动画合并为一个具有有意义名称的文件,以便任何开发人员都能准确地了解将动画应用到FrameworkElement中的内容。下面是一个小例子:

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

要设置应用程序中已有的这组预配置AnimationSettings,请执行以下步骤:

  1. Project Engineering Right-click the menu and click ** Add> New Item... **
  2. 选择 Resource Dictionary 并命名为 Animations.xaml
  3. App.xaml内容如下:
<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Animations.xaml" />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>
  1. Animations.xaml中,复制粘贴以下相应链接中的内容

你的应用程序现在有一组通用动画可以使用了。

Default animation (WPF only)

除了创建包含自定义AnimationSettingsResourceDictionary之外,XamlFlair 还提供一些默认动画。

要在应用程序中引用这些默认动画,请在App.xaml中执行以下步骤:

  1. 顶部添加XamlFlair.WPF命名空间
xmlns:xf="clr-namespace:XamlFlair;assembly=XamlFlair.WPF"
  1. Update Application Resources:
<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <xf:XamlFlairResources />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

您的应用程序现在有一系列全局默认的动画可以使用了。

如果 Visual Studio Intellisense 在使用<xf:XamlFlairResources />时不起作用,您可能需要尝试以下操作:

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

TransformOn 属性 (仅 WPF)

RenderTransform可使用TransformOn属性应用动画。可用选项为RenderLayout。未指定任何内容时,默认为Render。以下是关于两个选项的示例:

TransformOn动画

注意:非常重要的是要注意 WPF 的LayoutTransform不支持任何TranslateTransform,因此 translate 动画永远不会生效。您可以在这里的备注部分了解更多信息。

The original text www.example.com is too long and the translation is tiring. Everyone is interested in reading the original text. Finally, the previous picture:

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 9/13/2025

Migration from WPF to Avalonia series: Why I have to migrate WPF programs to Avalonia

In the past few years, our host computer software has been mainly developed using WPF and WinForm. These technologies are really easy to use on the Windows platform, and they have also accompanied us through the stage of small-scale trial production to today's large-scale delivery. However, with the development of business and changes in customer needs, the single Windows technology stack has gradually become a hurdle that we must overcome.

继续阅读
同分类 / 同标签 1/26/2025

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

继续阅读