易于功能在WPF动画自动关闭一段时间后, [英] Ease function is automatically turned off in wpf animation after some time

查看:304
本文介绍了易于功能在WPF动画自动关闭一段时间后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF和C#。动画在C#code。
我有许多点(> 1000)的路径动画。动画编程方式使用自如功能的一个故事板和动画点创建。重复行为设置为永远。一段时间后,便于功能关闭。我认为这是某种optimalization的。我可以关闭此功能,这样便于功能也会被设置到永远吗?

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?

这是我的mainwindow_loaded功能。我只有一个名为格一个表格在我的XAML code。

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

尝试把+ r.Next(200)code,使线看起来像

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.

推荐答案

我不知道任何方式,这将有所帮助,但为什么不使用共享实例的easingFunction,而不是为每一个动画创建一个单独的对象?从code,似乎他们都是一样的吗?

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?

我注意到一件事是,你是immidiately开始你的动画在加载的回调,但在我的WPF和Silverlight体验Loaded事件是该框架非常特殊的,你应该非常小心你在里面做什么。如何调用故事板一点点延迟这样的Begin方法:

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

这篇关于易于功能在WPF动画自动关闭一段时间后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆