繁体   English   中英

一段时间后,WPF动画中的缓动功能会自动关闭

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

WPF和c#。 C#代码中的动画。 我有许多点(> 1000)的路径动画。 使用一个故事板以编程方式创建动画,并具有轻松功能的点动画。 重复行为设置为永远。 一段时间后,缓解功能关闭。 我认为这是某种优化。 我可以关闭此功能,以便永久设置缓动功能吗?

这是我的mainwindow_loaded函数。 我的xaml代码中只有一个名为grid的Grid。

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

尝试在代码中加上+ r.Next(200),使该行看起来像

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

然后缓动功能将永远不会消失。

我不确定这是否有帮助,但是为什么不为EasingFunction使用共享实例,而不是为每个动画创建单独的对象? 从您的代码看来,它们都是一样的吗?

我注意到的另一件事是,您将在Loaded回调中立即启动动画,但是根据我使用WPF和Silverlight的经验,Loaded事件对于框架非常特殊,因此在执行该操作时应格外小心。 这样调用故事板的Begin方法怎么样有点延迟:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM