0

Here is my code which i think is correct. But it's not working. Is this some kind of bug or I did something wrong? Animating line segment from xaml works just fine.

MainWindow.xaml:

<Window x:Class="XXX.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Canvas Name="canvas">

</Canvas>
</Window>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        anim();
    }

    void anim()
    {
        Path path = new Path { Stroke = Brushes.Red, StrokeThickness = 1 };
        PathGeometry pg = new PathGeometry();
        PathFigureCollection pfc = new PathFigureCollection();
        PathFigure pf = new PathFigure { StartPoint = new Point(50, 50) };

        Storyboard sb = new Storyboard 
        { 
            Duration = System.Windows.Duration.Forever, 
            BeginTime = new TimeSpan(0, 0, 0), 
            RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever, 
            Name = "sb" 
        };

        LineSegment ls = new LineSegment 
        { 
            IsSmoothJoin = true, 
            Point = new Point(80, 50) 
        };

        PointAnimation pa = new PointAnimation
        {
            BeginTime = new TimeSpan(0, 0, 0, 0, 0),
            Duration = new Duration(new TimeSpan(0, 0, 0, 1)),
            From = new Point(0, 0),
            To = new Point(0, 100),
            RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever,
            AutoReverse = true,

        };

        Storyboard.SetTarget(pa, ls);
        Storyboard.SetTargetProperty(pa, new PropertyPath("Point"));

        sb.Children.Add(pa);

        pf.Segments.Add(ls);

        pfc.Add(pf);
        pg.Figures = pfc;
        path.Data = pg;
        canvas.Children.Add(path);

        sb.Begin();


    }
}

For me it looks like sb.Begin(); doesn't fire or storyboard.settarget/setproperty is set incorrectly. But what's wrong if this is the case?

masterkawaster
  • 199
  • 1
  • 8

1 Answers1

1

Ok it is stupid for me but with this code instead of sb.begin() it works:

int count = 0;
        foreach(var f in pf.Segments)
        {                
            f.BeginAnimation(LineSegment.PointProperty, (PointAnimation)sb.Children.ElementAt(count));
            count++;
        }

LineSegment has no method BeginStoryboard. http://msdn.microsoft.com/en-us/library/system.windows.media.linesegment.aspx

It is not a framework element and storyboard begin method work only for framework elements...

http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.controls.wpf.leftrightplacementtooltip.beginstoryboard(v=vs.110).aspx

I cannot see any logical explanation why if something is System.Windows.Media.Animation.Animatable cannot be controlled by using storyboards... I think the mechanism should be corrected.

masterkawaster
  • 199
  • 1
  • 8
  • I found the other solution also - see my next question: http://stackoverflow.com/questions/22994777/turn-off-optimizing-wpf-animation – masterkawaster Apr 10 '14 at 18:48