Пример #1
0
        public static Task StaggeredChildren(
            VisualElement rootElement,
            AnimationDefinition animation,
            int initialDelay, int staggeredDelay, bool clear)
        {
            var list = new List <Task>();

            var delay    = initialDelay;
            var elements = rootElement.LogicalChildren;

            foreach (var element in elements.OfType <VisualElement>())
            {
                var child = animation.Clone();
                child.PauseBefore = delay;

                if (clear)
                {
                    element.ClearTransforms(0);
                }
                list.Add(element.Animate(child));
                delay += staggeredDelay;
            }

            return(Task.WhenAll(list));
        }
        public static Task <bool> Animate(this VisualElement visualElement, AnimationDefinition animationDefinition)
        {
            var tcs         = new TaskCompletionSource <bool>();
            var animation   = animationDefinition.CreateAnimation(visualElement);
            var animationId = visualElement.Id.ToString();

            // Prevent any opacity challenges when element starts hidden
            if (animationDefinition.OpacityFromZero)
            {
                visualElement.Opacity   = 0;
                visualElement.IsVisible = true;
            }

            if (animationDefinition.PauseBefore > 0 ||
                animationDefinition.PauseAfter > 0 ||
                animationDefinition.RepeatCount > 1 ||
                animationDefinition.Delay > 0)
            {
                Task.Run(async() =>
                {
                    int remaining = animationDefinition.RepeatCount;

                    if (animationDefinition.PauseBefore > 0)
                    {
                        await Task.Delay(animationDefinition.PauseBefore);
                    }

                    animation.Commit(
                        owner: visualElement,
                        name: animationId,
                        rate: 16,
                        length: animationDefinition.Duration,
                        easing: null,
                        finished: async(f, cancelled) =>
                    {
                        if (!cancelled && animationDefinition.PauseAfter > 0)
                        {
                            await Task.Delay(animationDefinition.PauseAfter);
                        }

                        if (cancelled || remaining <= 0)
                        {
                            tcs.SetResult(cancelled);
                        }
                    },
                        repeat: () =>
                    {
                        remaining--;
                        return(remaining > 0);
                    });
                });
            }
            else
            {
                animation.Commit(
                    owner: visualElement,
                    name: animationId,
                    rate: 16,
                    length: animationDefinition.Duration,
                    easing: null,
                    finished: (f, a) => tcs.SetResult(a));
            }

            return(tcs.Task);
        }