Esempio n. 1
0
        public static void StopAnimation(this CompositionObject compositionObject, CompositionAnimation animation)
        {
            if (string.IsNullOrWhiteSpace(animation.Target))
                throw new ArgumentNullException("Animation has no target");

            compositionObject.StopAnimation(animation.Target);
        }
Esempio n. 2
0
        /// <summary>
        /// Starts an animation on the given property of a <see cref="CompositionObject"/>
        /// </summary>
        /// <typeparam name="T">The type of the property to animate</typeparam>
        /// <param name="target">The target <see cref="CompositionObject"/></param>
        /// <param name="property">The name of the property to animate</param>
        /// <param name="value">The final value of the property</param>
        /// <param name="duration">The animation duration</param>
        /// <returns>A <see cref="Task"/> that completes when the created animation completes</returns>
        public static Task StartAnimationAsync <T>(this CompositionObject target, string property, T value, TimeSpan duration)
            where T : unmanaged
        {
            // Stop previous animations
            target.StopAnimation(property);

            // Setup the animation to run
            KeyFrameAnimation animation;

            switch (value)
            {
            case float f:
                var scalarAnimation = target.Compositor.CreateScalarKeyFrameAnimation();
                scalarAnimation.InsertKeyFrame(1f, f);
                animation = scalarAnimation;
                break;

            case Color c:
                var colorAnimation = target.Compositor.CreateColorKeyFrameAnimation();
                colorAnimation.InsertKeyFrame(1f, c);
                animation = colorAnimation;
                break;

            case Vector4 v4:
                var vector4Animation = target.Compositor.CreateVector4KeyFrameAnimation();
                vector4Animation.InsertKeyFrame(1f, v4);
                animation = vector4Animation;
                break;

            default: throw new ArgumentException($"Invalid animation type: {typeof(T)}", nameof(value));
            }

            animation.Duration = duration;

            // Get the batch and start the animations
            var batch = target.Compositor.CreateScopedBatch(CompositionBatchTypes.Animation);

            var tcs = new TaskCompletionSource <object>();

            batch.Completed += (s, e) => tcs.SetResult(null);

            target.StartAnimation(property, animation);

            batch.End();

            return(tcs.Task);
        }
 /// <summary>
 /// Stops the given animation on the property specified by the given expression.
 /// The expression is converted to the appropriate property string by the
 /// CompositionExpressionEngine
 /// </summary>
 /// <param name="compositionObject">CompositionObject</param>
 /// <param name="expression">Expression defining the property on which to stop the animation</param>
 public static void StopAnimation(this CompositionObject compositionObject,
                                  Expression <Func <object> > expression)
 {
     compositionObject.StopAnimation(CompositionExpressionEngine.ParseExpression(expression));
 }