Exemplo n.º 1
0
        /// <summary>
        /// Called when the value of the
        /// <see cref="P:System.Windows.Controls.ContentControl.Content"/>
        /// property changes.
        /// </summary>
        /// <param name="oldContent">The old <see cref="T:System.Object"/>.</param>
        /// <param name="newContent">The new <see cref="T:System.Object"/>.</param>
        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            UIElement oldElement = oldContent as UIElement;
            UIElement newElement = newContent as UIElement;

            if (_firstContentPresenter == null || _secondContentPresenter == null || newElement == null)
            {
                return;
            }
            if (_useFirstAsNew)
            {
                _newContentPresenter = _firstContentPresenter;
                _oldContentPresenter = _secondContentPresenter;
            }
            else
            {
                _newContentPresenter = _secondContentPresenter;
                _oldContentPresenter = _firstContentPresenter;
            }
            _useFirstAsNew = !_useFirstAsNew;
            NavigationOutTransition navigationOutTransition = null;
            NavigationInTransition  navigationInTransition  = null;
            ITransition             oldTransition           = null;
            ITransition             newTransition           = null;

            if (oldElement != null)
            {
                navigationOutTransition = TransitionService.GetNavigationOutTransition(oldElement);
                TransitionElement oldTransitionElement = null;
                if (navigationOutTransition != null)
                {
                    oldTransitionElement = _isForwardNavigation ? navigationOutTransition.Forward : navigationOutTransition.Backward;
                }
                if (oldTransitionElement != null)
                {
                    oldTransition = oldTransitionElement.GetTransition(oldElement);
                    _oldContentPresenter.CacheMode        = new BitmapCache();
                    _oldContentPresenter.IsHitTestVisible = false;
                }
            }
            if (newElement != null)
            {
                navigationInTransition = TransitionService.GetNavigationInTransition(newElement);
                TransitionElement newTransitionElement = null;
                if (navigationInTransition != null)
                {
                    newTransitionElement = _isForwardNavigation ? navigationInTransition.Forward : navigationInTransition.Backward;
                }
                if (newTransitionElement != null)
                {
                    newElement.UpdateLayout();
                    newTransition = newTransitionElement.GetTransition(newElement);
                    _newContentPresenter.CacheMode        = new BitmapCache();
                    _newContentPresenter.IsHitTestVisible = false;
                }
            }
            _newContentPresenter.Opacity    = 0;
            _newContentPresenter.Visibility = Visibility.Visible;
            _newContentPresenter.Content    = newElement;
            _oldContentPresenter.Opacity    = 1;
            _oldContentPresenter.Visibility = Visibility.Visible;
            _oldContentPresenter.Content    = oldElement;
            if (oldTransition != null)
            {
                if (oldTransition.GetCurrentState() != ClockState.Stopped)
                {
                    oldTransition.Stop();
                }
                oldTransition.Completed += delegate
                {
                    oldTransition.Stop();
                    _oldContentPresenter.CacheMode        = null;
                    _oldContentPresenter.IsHitTestVisible = true;
                    if (navigationOutTransition != null)
                    {
                        navigationOutTransition.OnEndTransition();
                    }
                    TransitionNewElement(newTransition, navigationInTransition);
                };
                Dispatcher.BeginInvoke(delegate
                {
                    Dispatcher.BeginInvoke(delegate
                    {
                        if (navigationOutTransition != null)
                        {
                            navigationOutTransition.OnBeginTransition();
                        }
                        oldTransition.Begin();
                    });
                });
            }
            else
            {
                TransitionNewElement(newTransition, navigationInTransition);
            }
        }