WPF - 动画图像源更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29282137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - animate image source change
提问by Rob McCabe
I'm pretty new to WPF but I think what I need to do is relatively simple. I need to create an image "animation", where I am changing an image source every .25 of a second.
我对 WPF 很陌生,但我认为我需要做的相对简单。我需要创建一个图像“动画”,每 0.25 秒更改一个图像源。
I have a folder named "animation" with images 1 to 25 png live (named 1.png, 2.png... 25.png). Each image correlate to different frame of my animation.
我有一个名为“animation”的文件夹,其中包含 1 到 25 个 png 实时图像(名为 1.png、2.png ... 25.png)。每个图像都与我的动画的不同帧相关。
I want to write xaml to change the image from 1 to 2, from 2 to 3, for 3 to 4 etc every .25 seconds until it gets to the 25th image, then it should loop back to the start.
我想编写 xaml 以每 0.25 秒将图像从 1 更改为 2、从 2 更改为 3、从 3 更改为 4 等,直到它到达第 25 个图像,然后它应该循环回到开始。
I most likely will need to write some c# to do this. I want it to run on a thread that can interact with the UI (as in update the image) but not block the UI thread.
我很可能需要编写一些 c# 来做到这一点。我希望它在可以与 UI 交互(如更新图像)但不阻塞 UI 线程的线程上运行。
Thanks in advance!
提前致谢!
回答by Clemens
A pure XAML solution could look like this, of course with different images and timings.
纯 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>

