Storyboard animation is lagging

2019-06-10 22:56发布

问题:

i created a snow falling animation but it is little lagging, if i create this animation in blank new app it works fine but if try to build some more things and use it fully build app it starts lagging.

XAML

<Grid>
    <Canvas x:Name="ContentPanel" Height="768">

    </Canvas>
</Grid>

C#

using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Media;


public partial class MainPage : PhoneApplicationPage
{
    private static Random random;

    public MainPage()
    {
        InitializeComponent();
        random = new Random();
        this.StartFallingSnowAnimation();
    }

    private Ellipse GenerateEllipse()
    {
        Ellipse element = new Ellipse();
        element.Fill = new SolidColorBrush(Colors.Cyan);
        element.Height = 8.0;
        element.Width = 8.0;
        this.ContentPanel.Children.Add(element);
        return element;
    }

    private Storyboard CreateStoryboard(UIElement element, double to, double toLeft)
    {
        Storyboard result = new Storyboard();
        DoubleAnimation animation = new DoubleAnimation();
        animation.To = to;
        Storyboard.SetTargetProperty(animation, new PropertyPath("(Canvas.Top)"));
        Storyboard.SetTarget(animation, element);

        DoubleAnimation animationLeft = new DoubleAnimation();
        animationLeft.To = toLeft;
        Storyboard.SetTargetProperty(animationLeft, new PropertyPath("(Canvas.Left)"));
        Storyboard.SetTarget(animationLeft, element);

        result.Children.Add(animation);
        result.Children.Add(animationLeft);

        return result;
    }

    private void StartFallingSnowAnimation()
    {
        for (int i = 0; i < 20; i++)
        {
            Ellipse localCopy = this.GenerateEllipse();
            localCopy.SetValue(Canvas.LeftProperty, i * 30 + 1.0);

            double y = Canvas.GetTop(localCopy);
            double x = Canvas.GetLeft(localCopy);

            double speed = 5 * random.NextDouble();
            double index = 0;
            double radius = 30 * speed * random.NextDouble();

            localCopy.Opacity = .3 + random.NextDouble();

            CompositionTarget.Rendering += delegate (object o, EventArgs arg)
            {
                index += Math.PI / (180 * speed);

                if (y < this.ContentPanel.DesiredSize.Height)
                {
                    y += .3 + speed;
                }
                else
                {
                    y = -localCopy.Height;
                }

                Canvas.SetTop(localCopy, y);
                Canvas.SetLeft(localCopy, x + radius * Math.Cos(index));
                Storyboard animation = this.CreateStoryboard(localCopy, y, x + radius * Math.Cos(index));
                Storyboard.SetTarget(animation, localCopy);
                animation.Begin();

            };
        }
    }
}

it works perfectly in an empty app, but start lagging if use in app build with some more controls or in fully build app.