简体   繁体   English

同步WPF动画问题

[英]Synchronize WPF animation issue

I cannot to synchronize WPF animation of the position and size at the same time. 我无法同时同步位置和大小的WPF动画。

Please have a look at my code and let me know if something is wrong there. 请查看我的代码,并让我知道那里是否有问题。

Thank you! 谢谢!

 void AnimatePlugin(double fromTop, double toTop, double fromLeft, double toLeft,
            double fromH, double toH, double fromW, double toW, UIElement control)
        {
            Storyboard sb = new Storyboard();

            #region Top

            DoubleAnimation daTop = new DoubleAnimation();
            daTop.From = fromTop;
            daTop.To = toTop;
            daTop.Duration = new Duration(TimeSpan.FromSeconds(1));
            sb.Children.Add(daTop);
            Storyboard.SetTargetProperty(daTop, new PropertyPath("(Canvas.Top)"));
            Storyboard.SetTarget(daTop, control);
            #endregion

            #region Left

            DoubleAnimation daLeft = new DoubleAnimation();
            daLeft.From = fromLeft;
            daLeft.To = toLeft;
            daLeft.Duration = new Duration(TimeSpan.FromSeconds(1));
            sb.Children.Add(daLeft);
            Storyboard.SetTargetProperty(daLeft, new PropertyPath("(Canvas.Left)"));
            Storyboard.SetTarget(daLeft, control);
            #endregion

            #region Heigh

            DoubleAnimation daH = new DoubleAnimation();
            daH.From = fromH;
            daH.To = toH;
            daH.Duration = new Duration(TimeSpan.FromSeconds(1));
            sb.Children.Add(daH);
            Storyboard.SetTargetProperty(daH, new PropertyPath("(Canvas.Height)"));
            Storyboard.SetTarget(daH, control);
            #endregion

            #region Width

            DoubleAnimation daW = new DoubleAnimation();
            daW.From = fromW;
            daW.To = toW;
            daW.Duration = new Duration(TimeSpan.FromSeconds(1));
            sb.Children.Add(daW);
            Storyboard.SetTargetProperty(daW, new PropertyPath("(Canvas.Width)"));
            Storyboard.SetTarget(daW, control);
            #endregion

            sb.Completed += (w, r) => { control.UpdateLayout(); };
            sb.Begin();
        }

Only Canvas.Left and and Canvas.Top are attached properties....(and you are specifying them correctly with brackets). Canvas.LeftCanvas.Top是附加属性....(并且您使用方括号正确指定了它们)。

However, "Width" and "Height" are not attached properties they are base properties of FrameworkElement......just use PropertyPath("Width") and PropertyPath("Height") 但是,“宽度”和“高度”不是附加属性,它们是FrameworkElement的基本属性……只需使用PropertyPath("Width")PropertyPath("Height")

Instead of using a string to specify the path to the property, you can use the PropertyPath that takes a DependencyProperty type.....so use PropertyPath(Canvas.TopProperty) , PropertyPath(Canvas.LeftProperty) , PropertyPath(Canvas.WidthProperty) , PropertyPath(Canvas.HeightProperty) . 您可以使用采用DependencyProperty类型的PropertyPath而不是使用字符串来指定属性的路径。....因此,请使用PropertyPath(Canvas.TopProperty)PropertyPath(Canvas.LeftProperty)PropertyPath(Canvas.WidthProperty)PropertyPath(Canvas.HeightProperty)

This way you don't need to worry yourself with using the correct syntax for the PropertyPath string, which is different depending on if the property is attached or not. 这样,您无需担心为PropertyPath字符串使用正确的语法,这取决于是否附加属性而有所不同。

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

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