private void AnimateCooldown()
        {
            if (_context == null)
            {
                return;
            }
            _an.Duration = TimeSpan.FromMilliseconds(_context.DurationLeft);
            var fps = _context.DurationLeft > 20000 ? 1 : 10;

            Timeline.SetDesiredFrameRate(_an, fps);
            MainArcRef?.BeginAnimation(Arc.EndAngleProperty, _an);
        }
Example #2
0
        /// <summary>
        /// Coerce visibility
        /// </summary>
        /// <param name="dependencyObject">Dependency object</param>
        /// <param name="baseValue">Base value</param>
        /// <returns>Coerced value</returns>
        private static object CoerceVisibility(
            DependencyObject dependencyObject,
            object baseValue)
        {
            // Make sure object is a framework element
            FrameworkElement frameworkElement = dependencyObject as FrameworkElement;

            if (frameworkElement == null)
            {
                return(baseValue);
            }

            // Cast to type safe value
            Visibility visibility = (Visibility)baseValue;

            // If Visibility value hasn't change, do nothing.
            // This can happen if the Visibility property is set using data binding
            // and the binding source has changed but the new visibility value
            // hasn't changed.
            if (visibility == frameworkElement.Visibility)
            {
                return(baseValue);
            }

            // If element is not hooked by our attached property, stop here
            if (!IsHookedElement(frameworkElement))
            {
                return(baseValue);
            }

            // Update animation flag
            // If animation already started, don't restart it (otherwise, infinite loop)
            if (UpdateAnimationStartedFlag(frameworkElement))
            {
                return(baseValue);
            }

            // If we get here, it means we have to start fade in or fade out animation.
            // In any case return value of this method will be Visibility.Visible,
            // to allow the animation.
            DoubleAnimation doubleAnimation = new DoubleAnimation
            {
                Duration = new Duration(TimeSpan.FromMilliseconds(AnimationDuration))
            };

            // When animation completes, set the visibility value to the requested
            // value (baseValue)
            doubleAnimation.Completed += (sender, eventArgs) =>
            {
                if (visibility == Visibility.Visible)
                {
                    // In case we change into Visibility.Visible, the correct value
                    // is already set, so just update the animation started flag
                    UpdateAnimationStartedFlag(frameworkElement);
                }
                else
                {
                    // This will trigger value coercion again
                    // but UpdateAnimationStartedFlag() function will reture true
                    // this time, thus animation will not be triggered.
                    if (BindingOperations.IsDataBound(frameworkElement,
                                                      UIElement.VisibilityProperty))
                    {
                        // Set visiblity using bounded value
                        Binding bindingValue =
                            BindingOperations.GetBinding(frameworkElement,
                                                         UIElement.VisibilityProperty);
                        BindingOperations.SetBinding(frameworkElement,
                                                     UIElement.VisibilityProperty, bindingValue);
                    }
                    else
                    {
                        // No binding, just assign the value
                        frameworkElement.Visibility = visibility;
                    }
                }
            };

            if (visibility == Visibility.Collapsed || visibility == Visibility.Hidden)
            {
                // Fade out by animating opacity
                doubleAnimation.From = 1.0;
                doubleAnimation.To   = 0.0;
            }
            else
            {
                // Fade in by animating opacity
                doubleAnimation.From = 0.0;
                doubleAnimation.To   = 1.0;
            }

            // Start animation
            frameworkElement.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);

            // Make sure the element remains visible during the animation
            // The original requested value will be set in the completed event of
            // the animation
            return(Visibility.Visible);
        }
        public static void AnimateDouble(FrameworkElement element, DependencyProperty property, double to, TimeSpan duration)
        {
            DoubleAnimation double_animation = new DoubleAnimation(to, duration);

            element.BeginAnimation(property, double_animation);
        }