代码之家  ›  专栏  ›  技术社区  ›  Josh Santangelo

扩展wpf动画类的奥秘

  •  2
  • Josh Santangelo  · 技术社区  · 15 年前

    Silverlight在其动画时间线(如DoubleAnimation)上有一个名为easingFunction的属性,该属性允许您指定一个用于插值两个值的函数。即使是.net 4,我还是想把它移植到3.5。阅读后 this ,看起来很可行,但我有个奇怪的问题。

    我正在扩展DoubleAnimation,如下所示:

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

    注意,除了添加新属性之外,它没有做任何有趣的事情。

    然后我可以在一些代码中使用它:

    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();
    

    这段代码可以很好地运行动画。

    但是,如果取消注释设置easingfunction的行,动画将不再运行。调用了我的createInstanceCore方法,但从未调用getcurrentValue。奇怪的?

    1 回复  |  直到 13 年前
        1
  •  1
  •   Tim Cooper    13 年前

    只是想在黑暗中拍一张。尝试将属性的类型更改为简单类型( int / string )我怀疑这可能与你的财产类型是 EasingFunctionBase 你试图冻结这个实例。

    推荐文章