我已经设置了一个故事板来沿着定义的路径为椭圆设置动画,如下所示:http://msdn.microsoft.com/en-us/library/ms743217.aspx,进行一些修改以满足我的需要。但是,当我尝试运行我的程序时,它给了我一个AnimationException:
Cannot animate the 'Center' property on a 'System.Windows.Media.EllipseGeometry' using a 'System.Windows.Media.Animation.PointAnimationUsingPath'. For details see the inner exception.
内部异常仅表示:Value does not fall within the expected range.
。我之前从未使用过故事板,所以我不知道该怎么做。
Location current = null;// locationEnum.Current;
Point start = new Point();
var segment = new PolyBezierSegment();
foreach (var location in locations) {
if (current == null) {
current = location;
start = new Point(current.X, current.Y);
}
else {
//MessageBox.Show(location.X.ToString() + "," + location.Y.ToString());
segment.Points.Add(new Point(location.X, location.Y));
//current = location;
}
}
//MessageBox.Show(start.ToString());
var ellipseGeometry = new EllipseGeometry(start, 15, 15);
var ellipsePath = new System.Windows.Shapes.Path {
Fill = new SolidColorBrush(Colors.Red),
Data = ellipseGeometry,
Margin = new Thickness(15)
};
cnvMap.Children.Add(ellipsePath);
var animationPath = new PathGeometry();
var figure = new PathFigure();
figure.StartPoint = start;
figure.Segments.Add(segment);
animationPath.Figures.Add(figure);
animationPath.Freeze();
var animation = new PointAnimationUsingPath {
PathGeometry = animationPath,
Duration = TimeSpan.FromSeconds(10)
};
Storyboard.SetTarget(animation, ellipseGeometry);
Storyboard.SetTargetProperty(
animation, new PropertyPath(EllipseGeometry.CenterProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(animation);
ellipsePath.Loaded += (sender, e) => {
//MessageBox.Show("Horray");
storyboard.Begin();
};
答案 0 :(得分:1)
我无法解决您的问题,但我可以告诉您如何调试它:
选项1 :将代码替换为尽可能接近您已链接的示例代码(并且预期可以使用的代码)。然后慢慢开始应用您的更改,直到它不再有效。您所做的最后一次更改很可能是问题的原因。如果这没有帮助,请在此处发布代码,包含有问题的代码行的两个版本。
选项2 :告诉Visual Studio停止所有异常(菜单栏/调试/异常)并尝试捕获引发内部异常的时刻。检查调用堆栈和本地变量窗口,找出哪个值“超出预期范围”。