/// <summary> /// Stops the overlay. /// </summary> /// <param name="overlay">The overlay.</param> private static void StopOverlay(Overlay overlay) { if (StopFadeInTimer(overlay)) return; var storyboard = CreateStoryboard(overlay, 1, 0); storyboard.Completed += (sender, args) => overlay.IsActive = false; if (!overlay._lastShown.HasValue) { storyboard.Begin(); return; } var elapsedMilliseconds = (DateTime.Now - overlay._lastShown.Value).Duration().TotalMilliseconds; var minimumDuration = overlay.MinimumDuration; if (elapsedMilliseconds < minimumDuration) { var duration = minimumDuration - elapsedMilliseconds; overlay._fadeOutTimer = new DispatcherTimer(DispatcherPriority.Normal, overlay.Dispatcher) { Interval = TimeSpan.FromMilliseconds(duration) }; overlay._fadeOutTimer.Tick += (sender, args) => { storyboard.Begin(); StopFadeOutTimer(overlay); }; overlay._fadeOutTimer.Start(); } else { storyboard.Begin(); } }
private static Storyboard CreateStoryboard(Overlay overlay, double @from, double to) { var storyboard = new Storyboard(); var doubleAnimation = new DoubleAnimation { From = @from, To = to, Duration = TimeSpan.FromMilliseconds(overlay.AnimationSpeed) }; storyboard.Children.Add(doubleAnimation); Storyboard.SetTarget(doubleAnimation, overlay); Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity")); return storyboard; }
private static bool StopFadeOutTimer(Overlay overlay) { if (overlay._fadeOutTimer == null) return false; overlay._fadeOutTimer.Stop(); overlay._fadeOutTimer = null; return true; }
/// <summary> /// Begins enabling the overlay. /// </summary> /// <param name="overlay">The overlay.</param> private static void BeginOverlay(Overlay overlay) { overlay._lastShown = null; if (StopFadeOutTimer(overlay)) return; var delay = overlay.Delay; if (delay <= 0) { overlay.IsActive = true; CreateStoryboard(overlay, 0, 1).Begin(); return; } overlay._fadeInTimer = new DispatcherTimer(DispatcherPriority.Normal, overlay.Dispatcher) { Interval = TimeSpan.FromMilliseconds(delay) }; overlay._fadeInTimer.Tick += (sender, args) => { overlay.IsActive = true; CreateStoryboard(overlay, 0, 1).Begin(); overlay._lastShown = DateTime.Now; StopFadeInTimer(overlay); }; overlay._fadeInTimer.Start(); }