SetTarget vs RegisterName / SetTargetName

时间:2012-11-04 08:43:51

标签: c# wpf storyboard

这是一个简单的程序,可以动画Y2形状的Line属性。请注意,我使用SetTarget方法定位Line。这个程序运行正常。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SoGeneratingAnimatedLine
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var canvas = new Canvas();

            Content = canvas;

            var sb = new Storyboard();

            var line = new Line()
            {
                X1 = 10, Y1 = 10,
                X2 = 90, Y2 = 10,
                Stroke = Brushes.Black,
                StrokeThickness = 2
            };

            canvas.Children.Add(line);

            var animation = new DoubleAnimation(10, 90, new Duration(TimeSpan.FromMilliseconds(1000)));

            sb.Children.Add(animation);

            Storyboard.SetTarget(animation, line);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Line.Y2Property));

            MouseDown += (s, e) => sb.Begin(this);
        }
    }
}

这是一个类似的程序,它为EndPoint的{​​{1}}设置动画,LineGeometryData

Path

第二个版本工作。但是,如果我替换该行:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SoGeneratingAnimatedLine
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var canvas = new Canvas();

            Content = canvas;

            var sb = new Storyboard();

            var lineGeometry = 
                new LineGeometry(new Point(10, 10), new Point(90, 10));

            var path = new Path()
            {
                Stroke = Brushes.Black,
                StrokeThickness = 2,
                Data = lineGeometry
            };

            canvas.Children.Add(path);

            var animation =
                new PointAnimation(
                    new Point(90, 10),
                    new Point(90, 90),
                    new Duration(TimeSpan.FromMilliseconds(1000)));

            sb.Children.Add(animation);

            Storyboard.SetTarget(animation, lineGeometry);
            Storyboard.SetTargetProperty(animation, new PropertyPath(LineGeometry.EndPointProperty));

            MouseDown += (s, e) => sb.Begin(this);
        }
    }
}

使用:

Storyboard.SetTarget(animation, lineGeometry);

然后动画运行。

为什么第二个程序的RegisterName("geometry", lineGeometry); Storyboard.SetTargetName(animation, "geometry"); 版本不起作用?什么时候可以使用SetTarget而不是SetTarget / RegisterName组合?这两种方法有什么不同?

2 个答案:

答案 0 :(得分:1)

完全不需要故事板。只需直接在LineGeometry上调用BeginAnimation

lineGeometry.BeginAnimation(LineGeometry.EndPointProperty, animation);

答案 1 :(得分:0)

在用代码创建应用程序时,必须调用RegisterName才能正确连接应用程序的动画情节提要板。这是因为关键情节提要板属性之一TargetName使用运行时名称查找,而不是能够引用目标元素。即使可以通过代码中的引用访问该元素,也是如此。