WPF动画 - 移动控件 [英] WPF Animation - Moving a control

查看:155
本文介绍了WPF动画 - 移动控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决这个问题。我只是想在表单中设置控件的位置动画。我尝试设置边距动画(使用 ThicknessAnimation ),但这不起作用,所以现在我正在尝试设置 TranslateTransform ,它根本不移动窗口。我不做什么?



I've been trying to finger this out for a few days. I simply want to animate the position of a control in a form. I tried animating the margin (using a ThicknessAnimation), but that didn't work, so now I'm trying to animate a TranslateTransform, and it simply doesn't move the window. What am I not doing?

public enum MoveVector { X, Y, XY };

private void MoveTo(FrameworkElement control, MoveVector vector, double x, double y)
{
    try
    {
        Storyboard sb = new Storyboard();
        sb.AutoReverse = false;

        System.Windows.Point point = control.TransformToAncestor((FrameworkElement)control.Parent).Transform(new System.Windows.Point(0, 0));

        DoubleAnimation animY = null; 
        DoubleAnimation animX = null; 

        if (vector == MoveVector.Y || vector == MoveVector.XY)
        {
            animY = new DoubleAnimation(point.Y, y, new Duration(new TimeSpan(5000)), FillBehavior.Stop);
            animY.By = 1d;
            Timeline.SetDesiredFrameRate(animY, 30);
            Storyboard.SetTarget(animY, control);
            Storyboard.SetTargetProperty(animY, new PropertyPath(TranslateTransform.YProperty));
            sb.Children.Add(animY);
        }
        if (vector == MoveVector.X || vector == MoveVector.XY)
        {
            animX = new DoubleAnimation(point.X, x, new Duration(new TimeSpan(5000)), FillBehavior.Stop);
            animX.By = 1d;
            Timeline.SetDesiredFrameRate(animX, 30);
            Storyboard.SetTarget(animX, control);
            Storyboard.SetTargetProperty(animX, new PropertyPath(TranslateTransform.XProperty));
            sb.Children.Add(animX);
        }
        sb.Begin();
    }
    catch (Exception ex)
    {
        if (ex != null) {}
    }
}

推荐答案

文档似乎要求将故事板注册为资源才能正常运行,这意味着为其应用名称,并将其添加为资源在 MSDN [ ^ ],但我也不知道。



从StackOverFlow上的问题中获得的实现似乎为您提供了如何继续的答案:

It seems that the documentation requires the storyboard to be registered as a Resource in order to function properly, that means applying a name to it, and adding it as a Resource etc. This is quite well explained in MSDN[^], but I didnt know about it either.

Implementation taken from a question on StackOverFlow seem to give you the answer on how to proceed:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WpfAnnimationTransform
{

    public partial class MainWindow : Window
    {

        Canvas panel;
        public MainWindow()
        {
            InitializeComponent();
            MouseDown += DoDynamicAnimation;

            Content = panel = new Canvas();
        }

        void DoDynamicAnimation(object sender, MouseButtonEventArgs args)
        {
            for (int i = 0; i < 12; ++i)
            {
                var e = new Button { Width = 16, Height = 16 }; //new Ellipse { Width = 16, Height = 16, Fill = SystemColors.HighlightBrush };
                Canvas.SetLeft(e, Mouse.GetPosition(this).X);
                Canvas.SetTop(e, Mouse.GetPosition(this).Y);

                var tg = new TransformGroup();
                var translation = new TranslateTransform(30, 0);
                var translationName = "myTranslation" + translation.GetHashCode();
                RegisterName(translationName, translation);
                tg.Children.Add(translation);
                tg.Children.Add(new RotateTransform(i * 30));
                e.RenderTransform = tg;

                panel.Children.Add(e);

                var anim = new DoubleAnimation(3, 150, new Duration(new TimeSpan(0, 0, 0, 2, 0)))
                {
                    EasingFunction = new PowerEase { EasingMode = EasingMode.EaseOut }
                };

                var s = new Storyboard();
                Storyboard.SetTargetName(s, translationName);
                Storyboard.SetTargetProperty(s, new PropertyPath(TranslateTransform.YProperty));
                var storyboardName = "s" + s.GetHashCode();
                Resources.Add(storyboardName, s);

                s.Children.Add(anim);

                s.Completed +=
                    (sndr, evtArgs) =>
                    {
                        panel.Children.Remove(e);
                        Resources.Remove(storyboardName);
                        UnregisterName(translationName);
                    };
                s.Begin();
            }
        }
    }
}



代码取自 this [ ^ ]。也许有点太多的按钮;)


The code is taken from this[^]. A little too many buttons perhaps ;)


这篇关于WPF动画 - 移动控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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