views:

225

answers:

1

Silverlight has a property on its animation timelines (like DoubleAnimation) called EasingFunction which allows you to specify a function with which to interpolate two values. Even though it's coming in .NET 4, I'd like to port this to 3.5. After reading this, it seems pretty doable, but I'm having a weird issue.

I'm extending DoubleAnimation like so:

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 it's not doing anything 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();

That code runs the animation just fine.

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

+1  A: 

Just gonna take a shot in the dark here. Try changing the type of the property to a simple type (int / string). I suspect it may have something to do with the fact that your property type is EasingFunctionBase and you're trying to freeze the instance.

HTH, Kent

Kent Boogaart
I actually experimented with that before posting, and found that as long as I had simple property types, everything worked fine. It looks like you just can't have complex property types on Freezable objects."So how does Silverlight 3 do it?" I ask, and I find in Reflector that DoubleAnimation in SL3 is actually not Freezable like it is in WPF. Seems like I might not be able to do what I want to do in the obvious way.
Josh Santangelo
Unless I make EasingFunctionBase also Freezable. Then it works, though I won't be able to change properties on it later... but that may be ok in my case. Thanks for the reply.
Josh Santangelo