这是它的样子: http ://screencast.com/t/4cd1yQJReXt
小船图像应该遵循您为其绘制的路径。它确实有效 - 它遵循路径,但它从错误的地方开始。我无法弄清楚我的动画出了什么问题,我现在已经多次查看代码。应用程序的 XAML 非常简单,所有元素都包含在 Canvas Name="Canvas1" Background="Black" 中
这个画布是 Window 的子元素
下面是执行动画的代码。我知道我喂它的线是正确的。只是他们的参考点有点搞砸了。应该是 Canvas1 但不是。
NameScope.SetNameScope(this, new NameScope());
MatrixTransform buttonMatrixTransform = new MatrixTransform();
ship1.RenderTransform = buttonMatrixTransform;
this.RegisterName("ButtonMatrixTransform", buttonMatrixTransform);
PathGeometry animationPath = new PathGeometry();
PathFigure pFigure = new PathFigure();
pFigure.StartPoint = new Point(pathnodes.ElementAt<Line>(0).X1, pathnodes.ElementAt<Line>(0).Y1);
PolyLineSegment ls = new PolyLineSegment();
foreach (Line l in pathnodes)
{
ls.Points.Add(new Point(l.X1, l.Y1));
ls.Points.Add(new Point(l.X2, l.Y2));
}
pFigure.Segments.Add(ls);
animationPath.Figures.Add(pFigure);
animationPath.Freeze();
MatrixAnimationUsingPath matrixAnimation =
new MatrixAnimationUsingPath();
matrixAnimation.IsOffsetCumulative = false;
matrixAnimation.PathGeometry = animationPath;
matrixAnimation.Duration = TimeSpan.FromSeconds(5);
matrixAnimation.DoesRotateWithTangent = true;
Storyboard.SetTargetName(matrixAnimation, "ButtonMatrixTransform");
Storyboard.SetTargetProperty(matrixAnimation,
new PropertyPath(MatrixTransform.MatrixProperty));
Storyboard pathAnimationStoryboard = new Storyboard();
pathAnimationStoryboard.Children.Add(matrixAnimation);
pathAnimationStoryboard.Begin(this);
谢谢!