如何向后调用故事板动画?

时间:2012-03-01 05:40:37

标签: windows-phone-7 windows-mobile windows-phone-7.1

我有一个故事板动画,我想在某种条件下向后播放

storyboard1.autoreverse = true;

不是我想要的

就像我想要更改为&来自字段

    <Storyboard x:Name="Storyboard1" Completed="Storyboard1_Completed">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="imageBack1">
            <EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="90"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="imageBack1">
            <DiscreteObjectKeyFrame KeyTime="0:0:0.25">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
        <DoubleAnimation Duration="0:0:0.25" To="90" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="image1" d:IsOptimized="True"/>
    </Storyboard>

2 个答案:

答案 0 :(得分:0)

在框架中没有对此的支持,但是,您自己很容易做到:

private void Reverse(Storyboard sb)
{
  DoubleAnimation anim = sb.Children.OfType<DoubleAnimation>().Single();
  object temp = anim.From;
  anim.From = anim.To;
  anim.To = Temp;
}

您可以使用以下内容:

Reverse(this.Resources["Storyboard1"] as Storyboard);

答案 1 :(得分:0)

我要手动翻转故事板:

    // does not work.
    public static Storyboard Reverse(Storyboard storyboard)
    {
        Storyboard newStoryboard = new Storyboard();
        newStoryboard.SpeedRatio = storyboard.SpeedRatio;
        foreach (var unknownChild in storyboard.Children)
        {
            if (unknownChild is DoubleAnimation)
            {
                DoubleAnimation child = unknownChild as DoubleAnimation;
                DoubleAnimation newChild = new DoubleAnimation()
                {
                    Duration = child.Duration,
                    // AutoReverse, BeginTime, FillBehaviour, RepeatBehaviour, SpeedRatio?
                    From = child.To,
                    To = child.From,
                    EasingFunction = child.EasingFunction
                };
                Storyboard.SetTargetProperty(newChild, Storyboard.GetTargetProperty(child));
                Storyboard.SetTargetName(newChild, Storyboard.GetTargetName(child));
                newStoryboard.Children.Add(newChild);
            }
            else if (unknownChild is ObjectAnimationUsingKeyFrames)
            {
                ObjectAnimationUsingKeyFrames child = unknownChild as ObjectAnimationUsingKeyFrames;
                var newChild = new ObjectAnimationUsingKeyFrames()
                {
                    Duration = (child as Timeline).Duration,
                };
                foreach (ObjectKeyFrame keyFrame in child.KeyFrames)
                {
                    var newKeyFrame = new DiscreteObjectKeyFrame() {
                        KeyTime = KeyTime.FromTimeSpan(child.Duration.TimeSpan - keyFrame.KeyTime.TimeSpan),
                        Value = keyFrame.Value
                    };
                    newChild.KeyFrames.Add(newKeyFrame);
                }
                Storyboard.SetTargetName(newChild, Storyboard.GetTargetName(child));
                Storyboard.SetTargetProperty(newChild, Storyboard.GetTargetProperty(child));
                newStoryboard.Children.Add(newChild);
            }
            else
            {
                throw new Exception("type " + unknownChild + " not supported");
            }
        }
        return newStoryboard;
    }

然而,主要问题似乎是即使我传输TargetName和TargetProperty值,新的故事板也找不到正确的元素。

不幸的是我不知道Storyboard类如何在Xaml Tree /它的资源父级中找出它的位置:/