如何在C#中的文本框上编码旋转类型动画而不使用标记

时间:2012-03-29 17:30:07

标签: c# wpf animation

我看到了一个示例,说明当图像在标记中获得焦点时如何创建发光效果。 下面是我发现的C#代码示例,它使用双动画来表示矩形的不透明度,我需要使用C#执行发光或旋转效果而不是标记,因为我不熟悉它。

        DoubleAnimation myDoubleAnimation = new DoubleAnimation();
        myDoubleAnimation.From = 1.0;
        myDoubleAnimation.To = 0.0;
        myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
        myDoubleAnimation.AutoReverse = true;
        myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;

        myStoryboard = new Storyboard();
        myStoryboard.Children.Add(myDoubleAnimation);
        Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
        Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.OpacityProperty));

1 个答案:

答案 0 :(得分:0)

您可以在InitializeComponent();

之后在Main()中添加它
textBlock1.Text = "Mouse over me";

var effect = new DropShadowEffect();
effect.Color = Colors.Red;
effect.BlurRadius = 10d;
effect.Opacity = 0d;
effect.ShadowDepth = 0d;
textBlock1.Effect = effect;

textBlock1.MouseEnter += (s, e) => {
    var anim = new DoubleAnimation(0d, 1d, new Duration(TimeSpan.FromMilliseconds(500)));
    effect.BeginAnimation(DropShadowEffect.OpacityProperty, anim); };

textBlock1.MouseLeave += (s, e) => {
    var anim = new DoubleAnimation(1d, 0d, new Duration(TimeSpan.FromMilliseconds(500)));
    effect.BeginAnimation(DropShadowEffect.OpacityProperty, anim); };

但我建议不要再用C#浪费时间来处理GUI代码,XAML会更加方便。