Ease function is automatically turned off in wpf animation after some time

不羁的心 提交于 2019-12-25 02:26:36

问题


WPF and c#. Animation in c# code. I have animation of path with many points (>1000). Animation is created programatically using one storyboard and point animations with ease function. Repeat behavior is set to forever. After some time ease function turns off. I think it's some kind of optimalization. Can I turn this off so that ease function will be set also forever?

This is my mainwindow_loaded function. I have only one Grid named grid in my xaml code.

void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Random r = new Random(10);

        Storyboard sb = new Storyboard();

        Path p = new Path
        {
            Width = 500,
            Height = 500,

            Name = "Path",
            Fill = Brushes.Green,
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
            Margin = new Thickness(25, 25, 0, 0),
            VerticalAlignment = System.Windows.VerticalAlignment.Top
        };

        PathGeometry pg = new PathGeometry();
        var pf = new PathFigure { StartPoint = new Point(0, 250) };

        for (int i = 0; i < this.ActualWidth; i++)
        {
            LineSegment ls = new LineSegment { Point = new Point(i, 50) };
            var pa = new PointAnimation
            {
                To = new Point(i, 80),
                Duration = new Duration(new TimeSpan(0, 0, 0, 2)),
                BeginTime = new TimeSpan(0, 0, 0, 0, i * 10),  //+ r.Next(200)
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever,
                EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut }
            };

            Storyboard.SetTargetProperty(pa, new PropertyPath("(Path.Data).(PathGeometry.Figures)[0].(PathFigure.Segments)[" + i + "].(LineSegment.Point)"));
            Storyboard.SetTarget(pa, p);

            sb.Children.Add(pa);

            pf.Segments.Add(ls);
        }

        LineSegment endSegment = new LineSegment { Point = new Point(this.ActualWidth, 250) };
        pf.Segments.Add(endSegment);

        pg.Figures.Add(pf);

        p.Data = pg;

        grid.Children.Add(p);

        sb.Begin();
    }

Try to put + r.Next(200) in code so that the line will look like

BeginTime = new TimeSpan(0, 0, 0, 0, i * 10 + r.Next(200)),

And then ease function will never disappear.


回答1:


I'm not sure in any way that this will help, but why not use a shared instance for the EasingFunction instead of creating a separate object for every single animation? From your code it seems they are all the same?

One more thing I noticed is that you are starting your animation immidiately in the Loaded callback, but in my experience with WPF and Silverlight the Loaded event is very special for the framework and you should be very careful with what you do in it. How about calling the Begin method of the storyboard a little bit delayed like this:

this.Dispatcher.BeginInvoke(new Action(() => db.BeginStoryboard()));


来源:https://stackoverflow.com/questions/22994777/ease-function-is-automatically-turned-off-in-wpf-animation-after-some-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!