Exemplo n.º 1
0
        /// <summary>
        /// Called when the timer has elapsed. Removes any stale notifications.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
        private static void OnTimerElapsed(WindowInfo windowInfo)
        {
            DateTime now = DateTime.Now;

            if (windowInfo.Window.IsMouseOver)
            {
                Observable
                .Timer(windowInfo.DisplayDuration)
                .ObserveOnDispatcher()
                .Subscribe(x => OnTimerElapsed(windowInfo));
            }
            else
            {
                BehaviorCollection behaviors     = Interaction.GetBehaviors(windowInfo.Window);
                FadeBehavior       fadeBehavior  = behaviors.OfType <FadeBehavior>().First();
                SlideBehavior      slideBehavior = behaviors.OfType <SlideBehavior>().First();

                fadeBehavior.FadeOut();
                slideBehavior.SlideOut();

                EventHandler eventHandler = null;
                eventHandler = (sender2, e2) =>
                {
                    fadeBehavior.FadeOutCompleted -= eventHandler;
                    windows.Remove(windowInfo);
                    windowInfo.Window.Close();
                };
                fadeBehavior.FadeOutCompleted += eventHandler;
            }
        }
        /// <summary>
        /// The animate out.
        /// </summary>
        public void AnimateOut()
        {
            if (this.AssociatedObject.Items.Count == 0)
            {
                this.OnAnimateOutCompleted(this, EventArgs.Empty);
            }
            else
            {
                if (this.AssociatedObject.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
                {
                    TimeSpan beginTime = this.Interval;

                    FrameworkElement selectedItem = GetSelectedItem(this.AssociatedObject);

                    foreach (object item in this.AssociatedObject.Items.Cast <object>().Reverse())
                    {
                        FrameworkElement frameworkElement = (FrameworkElement)this.AssociatedObject.ItemContainerGenerator.ContainerFromItem(item);
                        if (frameworkElement != null)
                        {
                            bool isFirst = this.AssociatedObject.Items.IndexOf(item) == 0;

                            if (this.IsRandom)
                            {
                                if (isFirst)
                                {
                                    beginTime = new TimeSpan(0, 0, 0, 0, 7 * 35);
                                }
                                else
                                {
                                    beginTime = new TimeSpan(0, 0, 0, 0, this.random.Next(0, 7) * 35);
                                }
                            }
                            else
                            {
                                beginTime = beginTime + this.Interval;
                            }

                            FadeBehavior fadeBehavior = GetFadeBehavior(
                                frameworkElement,
                                beginTime,
                                this.Duration);
                            SlideBehavior slideBehavior = GetSlideBehavior(
                                frameworkElement,
                                beginTime,
                                this.Duration,
                                this.Offset);

                            if (isFirst)
                            {
                                if (item != selectedItem)
                                {
                                    EventHandler eventHandler = null;
                                    eventHandler = (sender, e) =>
                                    {
                                        slideBehavior.SlideOutCompleted -= eventHandler;
                                        this.OnAnimateOutCompleted(this, EventArgs.Empty);
                                    };
                                    slideBehavior.SlideOutCompleted += eventHandler;
                                }
                                else
                                {
                                    EventHandler eventHandler = null;
                                    eventHandler = (sender, e) =>
                                    {
                                        fadeBehavior.FadeOutCompleted -= eventHandler;
                                        this.OnAnimateOutCompleted(this, EventArgs.Empty);
                                    };
                                    fadeBehavior.FadeOutCompleted += eventHandler;
                                }
                            }

                            fadeBehavior.FadeOut();

                            // Do not animate out the selected item.
                            if (item != selectedItem)
                            {
                                slideBehavior.SlideOut();
                            }
                        }
                    }
                }
                else
                {
                    EventHandler eventHandler = null;
                    eventHandler =
                        (sender, e) =>
                    {
                        this.AssociatedObject.ItemContainerGenerator.StatusChanged -= eventHandler;
                        this.AnimateIn();
                    };
                    this.AssociatedObject.ItemContainerGenerator.StatusChanged += eventHandler;
                }
            }
        }