The mysteries of extending the WPF animation classes
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?
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.
精彩评论