在WPF中动画的重复之间暂停

本文关键字:之间 暂停 WPF 动画 | 更新日期: 2023-09-27 18:02:39

我将以下FadeIn/FadeOut动画应用于WPF中的Canvas

var fadingInOutAnimation = new DoubleAnimation
{
    From = 1,
    To = 0,
    Duration = new Duration(TimeSpan.FromMilliseconds(1000)),
    AutoReverse = true,
    RepeatBehavior = RepeatBehavior.Forever,
};
MyCanvas.BeginAnimation(OpacityProperty, fadingInOutAnimation);

现在我想让它在动画结束时暂停1秒,然后再次重复。

就像这样:

Animation --- Pause (1 Sec) --- Animation --- Pause (1 Sec) and so on.

在WPF中动画的重复之间暂停

您可以添加一个故事板,它可以自动反转和重复,但它的持续时间比动画长:

var fadeInOutAnimation = new DoubleAnimation()
{
    From = 1,
    To = 0,
    Duration = TimeSpan.FromSeconds(1),
};
var storyboard = new Storyboard
{
    Duration = TimeSpan.FromSeconds(2),
    AutoReverse = true,
    RepeatBehavior = RepeatBehavior.Forever
};
Storyboard.SetTarget(fadeInOutAnimation, MyCanvas);
Storyboard.SetTargetProperty(fadeInOutAnimation,
                             new PropertyPath(Canvas.OpacityProperty));
storyboard.Children.Add(fadeInOutAnimation);
MyCanvas.BeginStoryboard(storyboard);

尝试在动画中使用BeginTime来启动循环之间的暂停。

<Storyboard TargetProperty ="(Fill).(SolidColorBrush.Color)"
            BeginTime ="00:00:00"
            RepeatBehavior ="Forever"
            AutoReverse="True">
<ColorAnimation From="Red" To="Yellow" 
            BeginTime="00:00:02"
            Duration="00:00:01"/>
</Storyboard>