动态更改 TargetName DoubleAnimation c# xaml

Dynamically Changing TargetName DoubleAnimation c# xaml

我正在寻找一种解决方案来更改我后面的代码中的 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");
            }

您可以使用 Storyboard.SetTargetName(Timeline, String) Method 动态更改 DoubleAnimationTargetName 属性 的值。您需要为每个 DoubleAnimationUsingKeyFrames 添加一个 Name

例如:

<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();
}