WPF - 动态更改图像源的动画化

6
我对WPF还比较陌生,但我认为我需要做的事情相对简单。我需要创建一个图像“动画”,每0.25秒更改一次图像源。
我有一个名为“animation”的文件夹,其中包含1到25个png实时图像(命名为1.png、2.png... 25.png)。每个图像对应我的动画的不同帧。
我想编写XAML来在每0.25秒内从1到2、从2到3、从3到4等更改图像,直到达到第25个图像,然后它应该循环回到开头。
我很可能需要编写一些C#来完成这个过程。我希望它可以在与UI交互的线程上运行(即更新图像),但不会阻塞UI线程。
提前感谢您!
1个回答

12

一个纯XAML的解决方案可能是这样的,当然图片和时间会不同。

<Image>
    <Image.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard RepeatBehavior="Forever">
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source"
                                                   Duration="0:0:2">
                        <DiscreteObjectKeyFrame KeyTime="0:0:0">
                            <DiscreteObjectKeyFrame.Value>
                                <BitmapImage UriSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"/>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                        <DiscreteObjectKeyFrame KeyTime="0:0:1">
                            <DiscreteObjectKeyFrame.Value>
                                <BitmapImage UriSource="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"/>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Image.Triggers>
</Image>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接