Exemple #1
0
        /// <summary>
        /// Starts an animation for a DependencyProperty. The animation will
        /// begin when the next frame is rendered.
        /// </summary>
        /// <param name="dp">
        /// The DependencyProperty to animate.
        /// </param>
        /// <param name="animation">
        /// <para>The AnimationTimeline to used to animate the property.</para>
        /// <para>If the AnimationTimeline's BeginTime is null, any current animations
        /// will be removed and the current value of the property will be held.</para>
        /// <para>If this value is null, all animations will be removed from the property
        /// and the property value will revert back to its base value.</para>
        /// </param>
        /// <param name="handoffBehavior">
        /// Specifies how the new animation should interact with any current
        /// animations already affecting the property value.
        /// </param>
        public void BeginAnimation(DependencyProperty dp, AnimationTimeline animation, HandoffBehavior handoffBehavior)
        {
            if (dp == null)
            {
                throw new ArgumentNullException("dp");
            }

            if (!AnimationStorage.IsPropertyAnimatable(this, dp))
            {
        #pragma warning disable 56506 // Suppress presharp warning: Parameter 'dp' to this public method must be validated:  A null-dereference can occur here.
                throw new ArgumentException(SR.Get(SRID.Animation_DependencyPropertyIsNotAnimatable, dp.Name, this.GetType()), "dp");
        #pragma warning restore 56506
            }

            if (animation != null &&
                !AnimationStorage.IsAnimationValid(dp, animation))
            {
                throw new ArgumentException(SR.Get(SRID.Animation_AnimationTimelineTypeMismatch, animation.GetType(), dp.Name, dp.PropertyType), "animation");
            }

            if (!HandoffBehaviorEnum.IsDefined(handoffBehavior))
            {
                throw new ArgumentException(SR.Get(SRID.Animation_UnrecognizedHandoffBehavior));
            }

            if (IsSealed)
            {
                throw new InvalidOperationException(SR.Get(SRID.IAnimatable_CantAnimateSealedDO, dp, this.GetType()));
            }

            AnimationStorage.BeginAnimation(this, dp, animation, handoffBehavior);
        }
        public static void AnimatePropertyTo <T, R, AT>(this T element, Expression <Func <T, R> > p, R finalValue, double duration, bool autoReverse)
            where T : IAnimatable
            where AT : AnimationTimeline
        {
            AnimationTimeline animation = (AnimationTimeline)Activator.CreateInstance(typeof(AT));

            if (animation == null)
            {
                return;
            }

            var prop         = (p.Body as MemberExpression).Member.Name;
            var currentValue = p.Compile()(element);

            DependencyPropertyDescriptor dFrom = DependencyPropertyDescriptor.FromName("From", animation.GetType(), animation.GetType());

            animation.SetValue(dFrom.DependencyProperty, currentValue);

            DependencyPropertyDescriptor dTo = DependencyPropertyDescriptor.FromName("To", animation.GetType(), animation.GetType());

            animation.SetValue(dTo.DependencyProperty, finalValue);

            animation.Duration    = new Duration(TimeSpan.FromSeconds(duration));
            animation.AutoReverse = autoReverse;

            DependencyPropertyDescriptor d = DependencyPropertyDescriptor.FromName(prop, typeof(T), typeof(T));

            element.BeginAnimation(d.DependencyProperty, null);
            element.BeginAnimation(d.DependencyProperty, animation);
        }