在code创建一个WPF动画闪烁的背后 [英] Create a Blink animation in WPF in code behind

查看:214
本文介绍了在code创建一个WPF动画闪烁的背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一个闪烁动画应用到画布让我画上的所有对象会眨眼吧。

I want to apply a Blink animation to a Canvas so that all the objects I have drawn on it would blink with it.

我已经有点成功使用code,低于该改变的画布,而在透明度属性快速达到这种效果,但我有点不满意了。

I have been somewhat successful using the code below which changes the Opacity property of the Canvas rather fast to achieve this effect but I'm kinda not satisfied with it.

我想preFER没有任何淡出/淡入在我目前的code纯闪烁。我怎样才能做到这一点的正确方法?

I would prefer a pure blink without any FadeOut/FadeIn as in my current code. How can I do it the right way?

var blinkAnimation = new DoubleAnimation
{
    From = 1,
    To = 0
};

var blinkStoryboard = new Storyboard
{
    Duration = TimeSpan.FromMilliseconds(500),
    RepeatBehavior = RepeatBehavior.Forever,
    AutoReverse = true
};

Storyboard.SetTarget(blinkAnimation, MyCanvas);
Storyboard.SetTargetProperty(blinkAnimation, new PropertyPath(OpacityProperty));

blinkStoryboard.Children.Add(blinkAnimation);
MyCanvas.BeginStoryboard(blinkStoryboard);

也许我可以做到这一点使用 VisibilityProperty ,但我无法得到它的权利。

Maybe I can do that using the VisibilityProperty but I couldn't get it right.

推荐答案

您可以使用第二个动画用适当的的BeginTime

You might use a second animation with an appropriate BeginTime:

var switchOffAnimation = new DoubleAnimation
{
    To = 0,
    Duration = TimeSpan.Zero
};

var switchOnAnimation = new DoubleAnimation
{
    To = 1,
    Duration = TimeSpan.Zero,
    BeginTime = TimeSpan.FromSeconds(0.5)
};

var blinkStoryboard = new Storyboard
{
    Duration = TimeSpan.FromSeconds(1),
    RepeatBehavior = RepeatBehavior.Forever
};

Storyboard.SetTarget(switchOffAnimation, MyCanvas);
Storyboard.SetTargetProperty(switchOffAnimation, new PropertyPath(Canvas.OpacityProperty));
blinkStoryboard.Children.Add(switchOffAnimation);

Storyboard.SetTarget(switchOnAnimation, MyCanvas);
Storyboard.SetTargetProperty(switchOnAnimation, new PropertyPath(Canvas.OpacityProperty));
blinkStoryboard.Children.Add(switchOnAnimation);

MyCanvas.BeginStoryboard(blinkStoryboard);

这篇关于在code创建一个WPF动画闪烁的背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆