9

2 つのオブジェクト間に曲線のパスを描画するプロジェクトに取り組んでいます。現在、ベジェ曲線とアニメーションをいじるテスト コードを書いています。最初のテストは、終点 (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);

OK、これで線が点に折りたたまれました。次に、_rect1 から _rect2 (端点にある 2 つのオブジェクト) に移動して、その行をアニメーション化します。

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

美しく動作します。しかし、ストーリーボードでやろうとすると、何も得られません。ストーリーボード化されたコードは次のとおりです。

        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でライン/ベジェポイントをアニメーション化した経験のある人はいますか?

4

2 に答える 2

2

私はあなたのコードを作り直しました、そしてこれはうまくいきます:

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

それはそれを修正します:)ターゲットはコントロール自体である必要があるようです。

次のように、1 段階下に移動します。

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

... InvalidOperationException を返します。

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

于 2010-06-25T17:14:00.763 に答える
0

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 がうまくいくようです。

編集: 期間はデフォルトで自動に設定されているようです。他に何が思いつくか見ていきます。ご容赦ください.. :)

于 2010-06-25T16:56:14.707 に答える