SetTarget vs RegisterName / SetTargetName

Oto prosty program, który animujeY2 własność aLine kształt. Zauważ, że używamSetTarget metoda celowaniaLine. Ten program działa dobrze.

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

Oto podobny program, który animujeEndPoint aLineGeometry który jestData dlaPath:

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

Ta druga wersja taknie praca. Jeśli jednak zastąpię linię:

Storyboard.SetTarget(animation, lineGeometry);

z:

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

następnie animacja zostanie uruchomiona.

Dlaczego nieSetTarget działa wersja drugiego programu? Kiedy można z niego korzystaćSetTarget zamiast tegoRegisterName/SetTargetName combo? Jaka jest różnica w tych dwóch podejściach?

questionAnswers(1)

yourAnswerToTheQuestion