Secrets of Extending WPF Animation Classes

Silverlight has a property on its animation timing (such as DoubleAnimation) called EasingFunction that allows you to specify a function with which to interpolate two values. Even though it's included in .NET 4, I would like to port it to 3.5. After reading this , it looks pretty doable, but I have a weird problem.

I am extending DoubleAnimation as follows:

class EasingDoubleAnimation : DoubleAnimation
{
    protected override Freezable CreateInstanceCore()
    {
        return new EasingDoubleAnimation();
    }

    protected override double GetCurrentValueCore( double defaultOriginValue, double defaultDestinationValue, AnimationClock animationClock )
    {
        Debug.WriteLine( animationClock.CurrentProgress.Value );
        return base.GetCurrentValueCore( defaultOriginValue, defaultDestinationValue, animationClock );
    }

    public EasingFunctionBase EasingFunction
    {
        get { return ( EasingFunctionBase ) GetValue( EasingFunctionProperty ); }
        set { SetValue( EasingFunctionProperty, value ); }
    }

    public static readonly DependencyProperty EasingFunctionProperty =
        DependencyProperty.Register( "EasingFunction", typeof( EasingFunctionBase ), typeof( EasingDoubleAnimation ),
            new PropertyMetadata( null ) );
}

      

Note that this does nothing interesting other than adding a new property.

And then I can use it in some code:

Storyboard sb = new Storyboard();
EasingDoubleAnimation ease = new EasingDoubleAnimation();
//ease.EasingFunction = new BackEase();
Storyboard.SetTarget( ease, MyTarget );
Storyboard.SetTargetProperty( ease, new PropertyPath( "(Canvas.Left)" ) );
ease.To = 100;
ease.Duration = TimeSpan.FromSeconds( 3 );
sb.Children.Add( ease );
sb.Begin();

      

This code works great with animations.

But, if I uncomment the line that sets the EasingFunction, the animation no longer works. My CreateInstanceCore method gets called, but GetCurrentValue is never called. Weird?

+2


source to share


1 answer


Just take a picture in the dark. Try changing the property type to simple type ( int

/ string

). I suspect it might have something to do with what your property type EasingFunctionBase

is and you are trying to freeze the instance.



+1


source







All Articles