public static async Task SlideAndFadeInAsync(this FrameworkElement element, AnimationSlideDirection direction, bool firstLoad, float seconds = 0.3f, bool keepMargin = true, int size = 0) { var sb = new Storyboard(); switch (direction) { // Add slide from left animation case AnimationSlideDirection.Left: sb.AddSlideFromLeft(seconds, size == 0 ? element.ActualWidth : size, keepMargin: keepMargin); break; // Add slide from right animation case AnimationSlideDirection.Right: sb.AddSlideFromRight(seconds, size == 0 ? element.ActualWidth : size, keepMargin: keepMargin); break; // Add slide from top animation case AnimationSlideDirection.Top: sb.AddSlideFromTop(seconds, size == 0 ? element.ActualHeight : size, keepMargin: keepMargin); break; // Add slide from bottom animation case AnimationSlideDirection.Bottom: sb.AddSlideFromBottom(seconds, size == 0 ? element.ActualHeight : size, keepMargin: keepMargin); break; } sb.AddFadeIn(seconds); sb.Begin(element); if (seconds != 0 || firstLoad) { element.Visibility = Visibility.Visible; } await Task.Delay((int)(seconds * 1000)); }
/// <summary> /// Slides an element out /// </summary> /// <param name="element">The element to animate</param> /// <param name="direction">The direction of the slide (this is for the reverse slide out action, so Left would slide out to left)</param> /// <param name="seconds">The time the animation will take</param> /// <param name="keepMargin">Whether to keep the element at the same width during animation</param> /// <param name="size">The animation width/height to animate to. If not specified the elements size is used</param> /// <returns></returns> public static async Task SlideAndFadeOutAsync(this FrameworkElement element, AnimationSlideDirection direction, float seconds = 0.3f, bool keepMargin = true, int size = 0) { // Create the storyboard var sb = new Storyboard(); // Slide in the correct direction switch (direction) { // Add slide to left animation case AnimationSlideDirection.Left: sb.AddSlideToLeft(seconds, size == 0 ? element.ActualWidth : size, keepMargin: keepMargin); break; // Add slide to right animation case AnimationSlideDirection.Right: sb.AddSlideToRight(seconds, size == 0 ? element.ActualWidth : size, keepMargin: keepMargin); break; // Add slide to top animation case AnimationSlideDirection.Top: sb.AddSlideToTop(seconds, size == 0 ? element.ActualHeight : size, keepMargin: keepMargin); break; // Add slide to bottom animation case AnimationSlideDirection.Bottom: sb.AddSlideToBottom(seconds, size == 0 ? element.ActualHeight : size, keepMargin: keepMargin); break; } // Add fade in animation sb.AddFadeOut(seconds); // Start animating sb.Begin(element); // Make page visible only if we are animating if (seconds != 0) { element.Visibility = Visibility.Visible; } // Wait for it to finish await Task.Delay((int)(seconds * 1000)); // Make element invisible element.Visibility = Visibility.Hidden; }
private static ThicknessAnimation MakeThicknessAnimation(float duration, double offset, float deceleration, AnimationSlideLocation location, AnimationSlideDirection direction, bool keepMargin) { var from = new Thickness(); var to = new Thickness(); switch (location) { case AnimationSlideLocation.Left: switch (direction) { case AnimationSlideDirection.In: from = new Thickness(-offset, 0, keepMargin ? offset : 0, 0); to = new Thickness(0); break; case AnimationSlideDirection.Out: from = new Thickness(0); to = new Thickness(-offset, 0, keepMargin ? offset : 0, 0); break; } break; case AnimationSlideLocation.Right: switch (direction) { case AnimationSlideDirection.In: from = new Thickness(keepMargin ? offset : 0, 0, -offset, 0); to = new Thickness(0); break; case AnimationSlideDirection.Out: from = new Thickness(0); to = new Thickness(keepMargin ? offset : 0, 0, -offset, 0); break; } break; case AnimationSlideLocation.Top: switch (direction) { case AnimationSlideDirection.Down: from = new Thickness(0, -offset, 0, keepMargin ? offset : 0); to = new Thickness(0); break; case AnimationSlideDirection.Up: from = new Thickness(0); to = new Thickness(0, -offset, 0, keepMargin ? offset : 0); break; } break; case AnimationSlideLocation.Bottom: switch (direction) { case AnimationSlideDirection.Down: from = new Thickness(0); to = new Thickness(0, keepMargin ? offset : 0, 0, -offset); break; case AnimationSlideDirection.Up: from = new Thickness(0, keepMargin ? offset : 0, 0, -offset); to = new Thickness(0); break; } break; default: throw new ArgumentOutOfRangeException(nameof(direction), direction, null); } return(new ThicknessAnimation { Duration = new Duration(TimeSpan.FromSeconds(duration)), From = from, To = to, DecelerationRatio = deceleration }); }