这是一个动画形状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);
}
}
}
这是一个类似的程序,它对 a 的 a 进行动画处理EndPoint
,LineGeometry
它是Data
for a 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
/SetTargetName
组合使用?这两种方法有什么区别?