9 c# wpf animation bezier storyboard
我正在研究一个涉及在两个物体之间绘制弯曲路径的项目.目前,我一直在编写一些测试代码来使用贝塞尔曲线和动画.第一个测试只是将端点(Point3)从原点对象(矩形)移动到目标对象(另一个矩形),直线.这是设置实际行的代码:
connector = new Path();
connector.Stroke = Brushes.Red;
connector.StrokeThickness = 3;
PathGeometry connectorGeometry = new PathGeometry();
PathFigure connectorPoints = new PathFigure();
connectorCurve = new BezierSegment();
connectorPoints.StartPoint = new Point((double)_rect1.GetValue(Canvas.LeftProperty) + _rect1.Width / 2,
(double)_rect1.GetValue(Canvas.TopProperty) + _rect1.Height / 2);
connectorCurve.Point1 = connectorPoints.StartPoint;
connectorCurve.Point2 = connectorPoints.StartPoint;
connectorCurve.Point3 = connectorPoints.StartPoint;
connectorPoints.Segments.Add(connectorCurve);
connectorGeometry.Figures.Add(connectorPoints);
connector.Data = connectorGeometry;
MainCanvas.Children.Add(connector);
Run Code Online (Sandbox Code Playgroud)
好的,所以我们现在有一条线折叠到了一个点.现在,让我们为该行设置动画,从_rect1到_rect2(端点处的两个对象):
PointAnimation pointAnim = new PointAnimation();
pointAnim.From = connectorCurve.Point3;
pointAnim.To = new Point((double)_rect2.GetValue(Canvas.LeftProperty) + _rect2.Width / 2,
(double)_rect2.GetValue(Canvas.TopProperty) + _rect2.Height / 2);
pointAnim.Duration = new Duration(TimeSpan.FromSeconds(5));
board.Children.Add(pointAnim);
Run Code Online (Sandbox Code Playgroud)
工作得很漂亮.但是,当我尝试使用故事板时,我什么都没得到.这是故事板代码:
Storyboard board = new Storyboard();
PointAnimation pointAnim = new PointAnimation();
pointAnim.From = connectorCurve.Point3;
pointAnim.To = new Point((double)_rect2.GetValue(Canvas.LeftProperty) + _rect2.Width / 2,
(double)_rect2.GetValue(Canvas.TopProperty) + _rect2.Height / 2);
pointAnim.Duration = new Duration(TimeSpan.FromSeconds(5));
Storyboard.SetTarget(pointAnim, connectorCurve);
Storyboard.SetTargetProperty(pointAnim, new PropertyPath(BezierSegment.Point3Property));
board.Children.Add(pointAnim);
board.Begin();
Run Code Online (Sandbox Code Playgroud)
什么都没动.我怀疑我正在为SetTarget或SetTargetProperty提供什么问题,但似乎无法弄明白.有没有人有WPF动画线/ bezier点的经验?
http://msdn.microsoft.com/en-us/library/system.windows.media.animation.storyboard(VS.95).aspx说:
不要尝试在页面的构造函数中调用 Storyboard 成员(例如 Begin)。这将导致动画无提示地失败。
..如果你这样做的话!
该页面上的示例还设置Storyboard 对象的Duration属性。
最后是一个一般性提示,一旦您掌握了这些类型的 UI 对象和奇怪的 XAML 对象图的基础知识,最好将其放入 ResourceDictionary 中,并使用“Resources["Name"] as Storyboard”之类的内容稍后将其取回。
希望这有帮助:看起来缺少的 Duration 应该可以解决问题。
编辑:看起来持续时间默认设置为自动,我会看看我还能想出什么,请耐心等待..:)
归档时间: |
|
查看次数: |
4400 次 |
最近记录: |