简体   繁体   English

如何在Silverlight中设置zIndex属性的动画?

[英]How to animate zIndex property in Silverlight?

Doubleanimation does not work, because the typ is an integer. 双重动画无效,因为类型是整数。 Are there any Integeranimation class? 有没有整数动画课程? Or doing it in a different manner. 或以其他方式进行。 I'm not interested in static animations, but event-driven (preferably programmatic) animations. 我对静态动画不感兴趣,但是对事件驱动(最好是程序化)动画感兴趣。

public static void Swap(UIElement ui1, UIElement ui2, double seconds)
        {
            var z2 = (int)ui1.GetValue(Canvas.ZIndexProperty);
            var z3 = (int)ui2.GetValue(Canvas.ZIndexProperty);
            DoubleAnimation da1 = new DoubleAnimation(); 
            DoubleAnimation da2 = new DoubleAnimation();
            Duration d = new Duration(TimeSpan.FromSeconds(seconds));
            da1.Duration = d;
            da2.Duration = d;
            Storyboard sb = new Storyboard();
            Storyboard.SetTarget(da1, ui1);
            Storyboard.SetTarget(da2, ui2);
            Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.ZIndex)"));
            Storyboard.SetTargetProperty(da2, new PropertyPath("(Canvas.ZIndex)"));
            sb.Duration = d;
            sb.Children.Add(da1);
            sb.Children.Add(da2);
            da1.To = (double)z3;
            da2.To = (double)z2;
            sb.Begin();



        }

        private void Btn1_Click_1(object sender, RoutedEventArgs e)
        {
            var z2 = (int)Image2.GetValue(Canvas.ZIndexProperty);
            var z3 = (int)Image3.GetValue(Canvas.ZIndexProperty);

            Swap(Image2, Image3, 3); 

        }

You can animate discreet values using ObjectAnimationUsingKeyFrames with DiscreteObjectKeyFrame. 您可以使用带有DiscreteObjectKeyFrame的ObjectAnimationUsingKeyFrames设置离散值的动画。

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.ZIndex)" Storyboard.TargetName="MyObject">
   <DiscreteObjectKeyFrame KeyTime="0" Value="250"/>
</ObjectAnimationUsingKeyFrames>

or you can do it in code. 或者您也可以通过代码来实现。

You can't animate an int property. 您不能为int属性设置动画。 Think about what it would actually mean. 考虑一下它的实际含义。

What might come close (visually) is animating the Opacity. 在视觉上可能接近的是对不透明度进行动画处理。 And maybe change a Zindex at an animation-complete event. 并可能在动画完成事件时更改Zindex。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM