使用故事板更改图像的来源

use*_*830 1 animation xaml image storyboard

如何使用故事板更改图像的来源?我想创建一个小动画,为此我想更改持续时间为 0.5 秒的图像源,使其看起来像一个小动画。(有点像 Gif 动画:))有没有办法用 Xaml 中的故事板来做到这一点,或者是使用计时器从代码后面更改图像源的唯一方法?

esh*_*ham 6

标准动画只能作用于引擎可以设置动画的属性。图像的来源不是这样的属性,至少不是开箱即用的。

您可以做的是准备多个图像,并使用情节提要使用 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>
Run Code Online (Sandbox Code Playgroud)