Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF动画:如果to==from,则不调用完整回调_C#_Wpf_Animation - Fatal编程技术网

C# WPF动画:如果to==from,则不调用完整回调

C# WPF动画:如果to==from,则不调用完整回调,c#,wpf,animation,C#,Wpf,Animation,我想找到一种更简洁的方法,让动画在to==from时完全回调到fire。现在我通过在我的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 =

我想找到一种更简洁的方法,让动画在to==from时完全回调到fire。现在我通过在我的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) => {

    };

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

像这样的事情应该可以做到:

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;       
}

每次创建计时器并将其存储在类变量中时,都需要耗费大量资源。