Example #1
0
        /// <summary>
        /// Called when the <c>Navigating</c> event is invoked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="NavigatingCancelEventArgs"/> instance containing the event data.</param>
        /// <remarks>
        /// This method is public due to the fact that weak events are used. Otherwise, the navigation completed events
        /// could not be handled (because we unsubscribed from the _rootFrame) when navigating away to prevent memory
        /// leaks.
        /// <para />
        /// Please, do not call this method yourself, otherwise you can seriously ruin your apps.
        /// </remarks>
        public void OnNavigatingEvent(object sender, NavigatingCancelEventArgs e)
        {
            if (!CanHandleNavigation)
            {
                return;
            }

            var uriString = e.GetUriWithoutQueryInfo();

            _hasNavigatedButNotNavigatedAway = false;

#if WINDOWS_PHONE
            var uriWithoutParameters = GetUriWithoutParameters(_rootFrame.CurrentSource);
            if (string.CompareOrdinal(uriWithoutParameters, _url) != 0)
            {
                return;
            }
#endif

            OnNavigating(e);

            if (!e.Cancel && !HasHandledSaveAndCancelLogic)
            {
                if (NavigatingAwaySavesViewModel)
                {
                    SaveAndCloseViewModel();
                }
                else
                {
                    CancelAndCloseViewModel();
                }

                HasHandledSaveAndCancelLogic = true;

#if NETFX_CORE
                //_rootFrame.Navigating -= OnNavigatingEvent;
                //_rootFrame.Navigated -= OnNavigatedEvent;
#elif WINDOWS_PHONE
                //_rootFrame.BackKeyPress -= OnBackKeyPress;
                //_rootFrame.Navigating -= OnNavigatingEvent;
                //_rootFrame.Navigated -= OnNavigatedEvent;
#elif SILVERLIGHT
                _rootFrame.Navigating -= OnNavigatingEvent;
                _rootFrame.Navigated  -= OnNavigatedEvent;
#else
                Application.Current.Navigating -= OnNavigatingEvent;
                Application.Current.Navigated  -= OnNavigatedEvent;
#endif
            }

            _navigationComplete = false;
        }
Example #2
0
        /// <summary>
        /// Tombstones the application.
        /// </summary>
        private void TombstoneIfRequired(NavigatingCancelEventArgs e)
        {
            if (_isTombstoned)
            {
                return;
            }

            var uriString = e.GetUriWithoutQueryInfo();

            if (!uriString.StartsWith("app://external", StringComparison.InvariantCultureIgnoreCase))
            {
                return;
            }

            var viewModelAsViewModelBase = ViewModel as ViewModelBase;

            if (viewModelAsViewModelBase != null)
            {
                Tombstoning.SafeInvoke(this);

                switch (viewModelAsViewModelBase.TombstoningMode)
                {
                case TombstoningMode.Disabled:
                    break;

                case TombstoningMode.Manual:
                    viewModelAsViewModelBase.PrepareForTombstoneStateInternal(PhoneApplicationService.Current.State);
                    break;

                case TombstoningMode.Auto:
                    var tombstoneData = ((ViewModelBase)ViewModel).SerializeForTombstoning();
                    PhoneApplicationService.Current.State[TombstoneKey] = tombstoneData;

                    Log.Debug("Automatically created tombstone data with key '{0}'", TombstoneKey);
                    break;
                }

                Tombstoned.SafeInvoke(this);
            }

            _isTombstoned = true;
        }