2009-07-06 9 views
9

私は2つのオブジェクト間に曲線のパスを描画するプロジェクトに取り組んでいます。現在、ベジェ曲線とアニメーションを使って遊ぶためのテストコードをいくつか書いています。最初のテストでは、原点オブジェクト(矩形)から目的オブジェクト(別の矩形)まで直線で、端点(Point3)を移動するだけです。実際の行を設定するコードを次に示します。WPFアニメーション - ベジエ曲線点のアニメーション

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

[OK]をクリックします。

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

作品を美しく:今、(エンドポイントで2つのオブジェクトを)_rect2する_rect1行くから、その行をアニメーション化することができます。しかし、私がストーリーボードでそれをしようとすると、何も得られません。ストーリーボードのコードは次のとおりです。

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

何も動きません。私はSetTargetまたはSetTargetPropertyに何を与えているのかに問題があると思っていますが、それを理解できないようです。誰もWPFのライン/ベジエポイントをアニメーション化する経験がありますか?

+0

私はあなたの問題を解決するとは思えませんが、あなたはこの記事のインスピレーションを見つけることができます:http://www.japf.fr/?p=227 –

答えて

0

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.storyboard(VS.95).aspxは言う:

は、ページのコンストラクタ内(例えば、開始)ストーリーボードメンバーを呼び出そうとしないでください。これにより、アニメーションが黙って失敗することになります。

..あなたはそれをしていました!

このページのサンプルでも、StoryboardオブジェクトのDurationプロパティが設定されています。

最後に、これらの種類のUIオブジェクトと奇妙なXAMLオブジェクトグラフを使用した一般的なヒントを、ResourceDictionaryに配置し、 'Resources ["Name"]を後でそれを取り戻す。

希望のあること:欠けているように見えます。期間は、そのトリックを行う必要があります。

編集:期間はデフォルトで自動に設定されているように見える、私はあなたのコードを再作成:) ..私と一緒に負担してください、私が思い付くことができます他に何

2

が表示されますが、これは動作します:

Storyboard.SetTarget(pointAnim, connector); 
Storyboard.SetTargetProperty(pointAnim, new PropertyPath("Data.Figures[0].Segments[0].Point3")); 

それを修正しました。ターゲットはコントロールそのものである必要があるようです。このようなダウン一歩、行く

Storyboard.SetTarget(pointAnim, connectorGeometry); 
Storyboard.SetTargetProperty(pointAnim, new PropertyPath("Figures[0].Segments[0].Point3")); 

は...と、InvalidOperationExceptionを与える:パス「図中の

'[不明]' プロパティ値[0] .Segments [ 0] .Point3 'は' System.Windows.Media.PathFigure 'の不変のインスタンスを指します。

関連する問題