当我更改幻灯片放映等图像源时,如何实现 FadeIn 然后 FadeOut 图像。我的图像从本地和网络加载,并且数量是可变的。
谢谢
最佳答案
您可以编写一个扩展方法,通过将 Opacity
属性设置为 0 来淡出图像,然后设置 Source
属性,最后将不透明度设置为 1。
public static void ChangeSource(
this Image image, ImageSource source, TimeSpan fadeOutTime, TimeSpan fadeInTime)
{
var fadeInAnimation = new DoubleAnimation(1d, fadeInTime);
if (image.Source != null)
{
var fadeOutAnimation = new DoubleAnimation(0d, fadeOutTime);
fadeOutAnimation.Completed += (o, e) =>
{
image.Source = source;
image.BeginAnimation(Image.OpacityProperty, fadeInAnimation);
};
image.BeginAnimation(Image.OpacityProperty, fadeOutAnimation);
}
else
{
image.Opacity = 0d;
image.Source = source;
image.BeginAnimation(Image.OpacityProperty, fadeInAnimation);
}
}
关于wpf - WPF中图像的淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19706973/