简体   繁体   English

SetTarget vs RegisterName / SetTargetName

[英]SetTarget vs RegisterName/SetTargetName

Here's a simple program which animates the Y2 property of a Line shape. 这是一个简单的程序,可以为LineY2属性设置动画。 Note that I use the SetTarget method to target the Line . 请注意,我使用SetTarget方法来定位Line This program works fine. 这个程序工作正常。

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);
        }
    }
}

Here's a similar program which animates the EndPoint of a LineGeometry which is the Data for a Path : 这是一个类似的程序,它动画了LineGeometryEndPoint ,它是PathData

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);
        }
    }
}

This second version does not work. 这第二个版本行不通的。 However, if I replace the line: 但是,如果我更换线路:

Storyboard.SetTarget(animation, lineGeometry);

with: 有:

RegisterName("geometry", lineGeometry);
Storyboard.SetTargetName(animation, "geometry");

then the animation runs. 然后动画运行。

Why doesn't the SetTarget version of the second program work? 为什么第二个程序的SetTarget版本不起作用? When is it OK to use SetTarget instead of the RegisterName / SetTargetName combo? 什么时候可以使用SetTarget而不是RegisterName / SetTargetName组合? What's the difference in the two approaches? 这两种方法有什么不同?

There is no need at all for a Storyboard. 故事板根本不需要。 Just call BeginAnimation directly on the LineGeometry: 只需在LineGeometry上直接调用BeginAnimation

lineGeometry.BeginAnimation(LineGeometry.EndPointProperty, animation);

Calling RegisterName is necessary in order to correctly hook up animation storyboards for applications when created in code. 调用RegisterName是必要的,以便在代码中创建应用程序时正确连接动画故事板。 This is because one of the key storyboard properties, TargetName , uses a run-time name lookup instead of being able to take a reference to a target element. 这是因为其中一个关键的故事板属性TargetName使用运行时名称查找,而不是能够引用目标元素。 This is true even if that element is accessible by reference from the code. 即使该元素可通过代码中的引用访问,也是如此。

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

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