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

时间:2014-05-10 20:35:26

标签: c# wpf animation

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

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.

1 个答案:

答案 0 :(得分:6)

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

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