我在WPF项目中有一个鱼的图像(后面是VB.net代码),我正在尝试使用两个变换来回动游戏。
出于某种原因,如果我只为ScaleTransform设置动画(仅使用ScaleTransform,而不使用TransformGroup),则动画效果正常,而TranslateTransform动画则不然。此外,ScaleTransform在TransformGroup内部时不起作用。
这是我正在使用的代码。我做错了什么?
<Image Height="90" HorizontalAlignment="Left" Name="Fish1" Stretch="Fill" VerticalAlignment="Top" Width="260" Source="/VBP-WORD4WORD;component/Images/IMG-FISH1.png" Canvas.Left="24" Canvas.Top="67" Margin="-28,70,0,0">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1"/>
<TranslateTransform X="0"/>
</TransformGroup>
</Image.RenderTransform>
<Image.Triggers>
<EventTrigger RoutedEvent="Image.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Duration="0:0:30" Storyboard.TargetProperty="RenderTransform.(TransformGroup.TranslateTransform.X)" RepeatBehavior="Forever">
<LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<LinearDoubleKeyFrame KeyTime="0:0:14.9" Value="407"/>
<LinearDoubleKeyFrame KeyTime="0:0:15" Value="680"/>
<LinearDoubleKeyFrame KeyTime="0:0:29.9" Value="265"/>
<LinearDoubleKeyFrame KeyTime="0:0:30" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Duration="0:0:30" Storyboard.TargetProperty="RenderTransform.(TransformGroup.ScaleTransform.ScaleX)" RepeatBehavior="Forever">
<LinearDoubleKeyFrame KeyTime="0:0:14.9" Value="1"/>
<LinearDoubleKeyFrame KeyTime="0:0:15" Value="-1"/>
<LinearDoubleKeyFrame KeyTime="0:0:29.9" Value="-1"/>
<LinearDoubleKeyFrame KeyTime="0:0:30" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Image.Triggers>
</Image>
答案 0 :(得分:17)
那些属性路径都是错误的,但我会投票通过利用Storyboard.TargetName
来避免仅使用这些路径的整个麻烦;这有效:
<!-- ... -->
<TransformGroup>
<ScaleTransform x:Name="scaleTransform" ScaleX="1"/>
<TranslateTransform x:Name="translateTransform" X="0"/>
</TransformGroup>
<!-- ... -->
<Storyboard>
<DoubleAnimationUsingKeyFrames Duration="0:0:30"
Storyboard.TargetProperty="ScaleX"
Storyboard.TargetName="scaleTransform"
RepeatBehavior="Forever">
<LinearDoubleKeyFrame KeyTime="0:0:14.9" Value="1"/>
<LinearDoubleKeyFrame KeyTime="0:0:15" Value="-1"/>
<LinearDoubleKeyFrame KeyTime="0:0:29.9" Value="-1"/>
<LinearDoubleKeyFrame KeyTime="0:0:30" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Duration="0:0:30"
Storyboard.TargetProperty="X"
Storyboard.TargetName="translateTransform"
RepeatBehavior="Forever">
<LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<LinearDoubleKeyFrame KeyTime="0:0:14.9" Value="407"/>
<LinearDoubleKeyFrame KeyTime="0:0:15" Value="680"/>
<LinearDoubleKeyFrame KeyTime="0:0:29.9" Value="265"/>
<LinearDoubleKeyFrame KeyTime="0:0:30" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
如果你真的只想使用Storyboard.TargetProperty
这样做,这些就是我刚才发现的正确路径:
Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX"
Storyboard.TargetProperty="RenderTransform.Children[1].X"
如果你考虑一下,那就非常有意义。