簡體   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