-1

我正在寻找一种解决方案来更改我后面的代码中的 TargetName 属性。我总共需要 9 种不同的图像/动画才能发生。在下面的示例中,只有 2 个。

这是 XAML

<Storyboard x:Name="MyAnimationBoard1" x:Key="MyAnimationBoard">
                    <DoubleAnimationUsingKeyFrames
                        Storyboard.TargetName="Anim1"
                        Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.5"/>
                        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:0.10"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.20"/>
                        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:0.30"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.40"/>
                    </DoubleAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames
                        Storyboard.TargetName="Anim2"
                        Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:4"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.5"/>
                        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:4.10"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.20"/>
                        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:4.30"/>
                        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.40"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>

在这里,您可以找到我在后面的代码中调用动画开始的方式

if (timesTicked == 59)
            {
                string voedsel = "Food1";
                Animate.AnimateFood(voedsel);
                //Would be nice to call the correct tagretname first.
                MyAnimationBoard1.Begin();
            }
            else if (timesTicked == 52)
            {
                string voedsel = "Food3";
                Animate.AnimateFood(voedsel);
                Debug.WriteLine("Tweede animatie");
                
            }
            else if (timesTicked == 46)
            {
                string voedsel = "Food8";
                Animate.AnimateFood(voedsel);
                Debug.WriteLine("Derde animatie");
            }
4

1 回答 1

0

您可以使用Storyboard.SetTargetName(Timeline, String) 方法来动态更改TargetName属性的值DoubleAnimation。您需要Name为每个DoubleAnimationUsingKeyFrames.

例如:

<Storyboard x:Name="MyAnimationBoard1" x:Key="MyAnimationBoard">
    <DoubleAnimationUsingKeyFrames x:Name="DoubleAnimation1"
                Storyboard.TargetName="Anim1"
                Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.5"/>
        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:0.10"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.20"/>
        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:0.30"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:0.40"/>
    </DoubleAnimationUsingKeyFrames>

    <DoubleAnimationUsingKeyFrames x:Name="DoubleAnimation2"
                Storyboard.TargetName="Anim2"
                Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)">
        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:4"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.5"/>
        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:4.10"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.20"/>
        <LinearDoubleKeyFrame Value="-30" KeyTime="0:0:4.30"/>
        <LinearDoubleKeyFrame Value="30" KeyTime="0:0:4.40"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>
//
private void Button_Click(object sender, RoutedEventArgs e)
{
    MyAnimationBoard1.Stop();
    Storyboard.SetTargetName(DoubleAnimation1, "Anim2");
    Storyboard.SetTargetName(DoubleAnimation2, "Anim3");
    MyAnimationBoard1.Begin();
}
于 2021-01-20T05:30:13.907 回答