如何使用故事板更改图像的来源?我想创建一个小动画,为此我想改变图像的来源,持续时间为0.5秒,看起来像一个小动画。 (有点像Gif-Animation :))有没有办法在Xaml中使用故事板进行此操作,或者是使用计时器从后面的代码更改图像源的唯一方法?
答案 0 :(得分:3)
标准动画只能用于引擎可以制作动画的属性。图像的来源不是这样的属性,至少不是开箱即用的。
你可以做的是准备几张图片,并使用故事板使用ObjectAnimationUsingKeyFrames在不同的关键帧上多次设置Source属性:
<Storyboard x:Key="Storyboard">
<ObjectAnimationUsingKeyFrames
Storyboard.TargetProperty="(Image.Source)"
Storyboard.TargetName="Image1">
<DiscreteObjectKeyFrame KeyTime="0:0:1">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="Images/1.jpeg" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:2">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="Images/2.jpeg" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>