1

我得到了这个 UserControl,它是一个大约 10 行和 10 列的网格。每个单元格包含 1 个文本块或 1-6 个图像。我想要做的是为每行上的所有元素设置动画以滑入。但是我找不到为整个 Grid.Row 设置动画的方法来做到这一点。

我也不能将所有元素包装在堆栈面板/画布中,因为这会将它们放在同一列中......

有人对此有解决方案吗?

谢谢

4

1 回答 1

1

所以我想出了一个可行的解决方案并且它正在工作。也许不是那么优雅,但它完成了它应该做的事情,并且在 Lumia 920 设备上运行良好。

void CreateLoadAnimationForRow(int row, int startTime)
    {
        foreach (var child in LayoutRoot.Children.Cast<FrameworkElement>().Where(x => Grid.GetRow(x) == row))
        {
            AddToStoryboard(child, startTime);
        }
    }
    void AddToStoryboard(FrameworkElement element, int startTime)
    {
        element.RenderTransform = new CompositeTransform();
        var doubleAnimation = new DoubleAnimation
        {
            From = -200,
            To = 0,
            Duration = new Duration(TimeSpan.FromSeconds(1)),
            BeginTime = TimeSpan.FromMilliseconds(startTime)
        };

        var objectAnimationUsingKeyFrames = new ObjectAnimationUsingKeyFrames()
        {
            BeginTime = TimeSpan.FromMilliseconds(startTime + 10)
        };
        var discreteObjectKeyFrame = new DiscreteObjectKeyFrame()
        {
            KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)),
            Value = Visibility.Visible
        };

        objectAnimationUsingKeyFrames.KeyFrames.Add(discreteObjectKeyFrame);

        IEasingFunction easingFunction = new SineEase();
        easingFunction.Ease(2);
        doubleAnimation.EasingFunction = easingFunction;

        Storyboard.SetTarget(objectAnimationUsingKeyFrames, element);
        Storyboard.SetTargetProperty(objectAnimationUsingKeyFrames, new PropertyPath("Visibility"));

        Storyboard.SetTarget(doubleAnimation, element);
        Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(FrameworkElement.RenderTransform).(CompositeTransform.TranslateX)"));

        storyboardSlideIn.Children.Add(doubleAnimation);
        storyboardSlideIn.Children.Add(objectAnimationUsingKeyFrames);

    }
于 2013-02-13T13:01:26.030 回答