public static void HideWithAnimation(this Window window) { if (hideAnimationInProgress) return; try { hideAnimationInProgress = true; var hideAnimation = new DoubleAnimation { Duration = new Duration(TimeSpan.FromSeconds(0.2)), FillBehavior = FillBehavior.Stop, EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseIn } }; var taskbarPosition = TaskbarService.GetWinTaskbarState().TaskbarPosition; switch (taskbarPosition) { case TaskbarPosition.Left: case TaskbarPosition.Right: hideAnimation.From = window.Left; break; default: hideAnimation.From = window.Top; break; } hideAnimation.To = (taskbarPosition == TaskbarPosition.Top || taskbarPosition == TaskbarPosition.Left) ? hideAnimation.From - 10 : hideAnimation.From + 10; hideAnimation.Completed += (s, e) => { window.Visibility = Visibility.Hidden; hideAnimationInProgress = false; }; switch (taskbarPosition) { case TaskbarPosition.Left: case TaskbarPosition.Right: window.ApplyAnimationClock(Window.LeftProperty, hideAnimation.CreateClock()); break; default: window.ApplyAnimationClock(Window.TopProperty, hideAnimation.CreateClock()); break; } } catch { hideAnimationInProgress = false; } }
public static UIElement FadeFromTo(this UIElement uiElement, double fromOpacity, double toOpacity, int durationInMilliseconds, bool loopAnimation, bool showOnStart, bool collapseOnFinish) { var timeSpan = TimeSpan.FromMilliseconds(durationInMilliseconds); var doubleAnimation = new DoubleAnimation(fromOpacity, toOpacity, new Duration(timeSpan)); if (loopAnimation) doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; uiElement.BeginAnimation(UIElement.OpacityProperty, doubleAnimation); if (showOnStart) { uiElement.ApplyAnimationClock(UIElement.VisibilityProperty, null); uiElement.Visibility = Visibility.Visible; } if (collapseOnFinish) { var keyAnimation = new ObjectAnimationUsingKeyFrames { Duration = new Duration(timeSpan) }; keyAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Collapsed, KeyTime.FromTimeSpan(timeSpan))); uiElement.BeginAnimation(UIElement.VisibilityProperty, keyAnimation); } return uiElement; }
public static void BeginAnimation(this IAnimatable animatable, DependencyProperty dependencyProperty, AnimationTimeline animation, HandoffBehavior handoffBehavior = HandoffBehavior.SnapshotAndReplace, object layerOwner = null) { AnimationTimelineClock animationClock = (AnimationTimelineClock)animation.CreateClock(); animatable.ApplyAnimationClock(dependencyProperty, animationClock, handoffBehavior, layerOwner); animationClock.Begin(animatable.RootClock); }
public static void ShowwithAnimation(this Window window) { if (showAnimationInProgress) return; try { showAnimationInProgress = true; window.Visibility = Visibility.Visible; window.Topmost = false; window.Activate(); var showAnimation = new DoubleAnimation { Duration = new Duration(TimeSpan.FromSeconds(0.3)), FillBehavior = FillBehavior.Stop, EasingFunction = new ExponentialEase { EasingMode = EasingMode.EaseOut } }; var taskbarPosition = TaskbarService.GetWinTaskbarState().TaskbarPosition; switch (taskbarPosition) { case TaskbarPosition.Left: case TaskbarPosition.Right: showAnimation.To = window.Left; break; default: showAnimation.To = window.Top; break; } showAnimation.From = (taskbarPosition == TaskbarPosition.Top || taskbarPosition == TaskbarPosition.Left) ? showAnimation.To - 25 : showAnimation.To + 25; showAnimation.Completed += (s, e) => { window.Topmost = true; showAnimationInProgress = false; window.Focus(); }; switch (taskbarPosition) { case TaskbarPosition.Left: case TaskbarPosition.Right: window.ApplyAnimationClock(Window.LeftProperty, showAnimation.CreateClock()); break; default: window.ApplyAnimationClock(Window.TopProperty, showAnimation.CreateClock()); break; } } catch { showAnimationInProgress = false; } }