gpt4 book ai didi

c# - 将连续动画应用于 WPF 中的 Canvas

转载 作者:行者123 更新时间:2023-11-30 22:06:31 25 4
gpt4 key购买 nike

我想在我的 WPF 中将连续的 FadeIn/FadeOut 动画应用到特定的 Canvas

我可以执行 FadeOut 部分,但是 FadeIn 部分会在那之后立即执行,并且破坏动画。

我还希望所有这一切都处于一个平稳的循环中,而不干扰我的正常操作。

将其视为动画背景。

执行此操作的最佳方法是什么?我应该使用 While 吗?还是应该使用 Timer? ...

var duration = new Duration(TimeSpan.FromMilliseconds(1000));
var fadeOut = new DoubleAnimation(0.0, duration);
var fadeIn = new DoubleAnimation(1.0, duration);
MyCanvas.BeginAnimation(OpacityProperty, fadeOut);
MyCanvas.BeginAnimation(OpacityProperty, fadeIn);

最佳答案

您可以设置单个 DoubleAnimation 的 AutoReverseRepeatBehavior 属性以获得连续效果:

var fadeInOutAnimation = new DoubleAnimation
{
From = 0,
To = 1,
Duration = TimeSpan.FromSeconds(1),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever,
};

MyCanvas.BeginAnimation(OpacityProperty, fadeInOutAnimation);

在您的方法中,您必须将第二个动画的 BeginTime 属性设置为第一个动画的 Duration

关于c# - 将连续动画应用于 WPF 中的 Canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com