WPF动画:完成未调用的回调if to == from

本文关键字:回调 if to from 调用 动画 WPF | 更新日期: 2023-09-27 18:06:38

我想找到一种更干净的方法来获得动画完整的回调,当到== from时触发。现在我通过在我的from值中添加少量来破解它。有没有更好的办法?

    //todo is there a way to get animations to call their Complete() even when to == from?
    if (to.Equals(from)) {
        from += .01;
    }
    DoubleAnimation animation = new DoubleAnimation {
        Name = axis == Axis.X ? TranslateTransform.XProperty.Name : TranslateTransform.YProperty.Name,
        From = from,
        To = to,
        Duration = TimeSpan.FromSeconds(translate.Time),
        FillBehavior = FillBehavior.Stop,
        EasingFunction = translate.Curve.ToEase(),
        IsAdditive = false,
    };
    AnimationClock = animation.CreateClock();
    AnimationClock.Completed += (sender, args) => {
    };

WPF动画:完成未调用的回调if to == from

您可以在创建动画之前检查值。
如果持续时间很重要,你可以启动一个计时器,并在它滴答作响时启动代码。

像这样的代码应该可以达到这个效果:

if (to.Equals(from)) {
        if (_timer == null)
        {
            _timer = new Timer(x =>
            {
                //completeCode
            }, null, translate.Time * 1000, Timeout.Infinite);
        }
        else
        {
            _timer.Change(translate.Time * 1000, Timeout.Infinite);
        }
    return;       
}