Ejemplo n.º 1
0
        /// <summary>
        /// Handles the OnNavigatingFrom event which notifies a page/ViewModel that we are
        /// moving AWAY from that view.
        /// </summary>
        /// <param name="rootFrame"></param>
        /// <param name="navMode"></param>
        /// <param name="cancel"></param>
        /// <param name="suspending"></param>
        private void HandleOnNavigatingFrom(Frame rootFrame, NavigationMode navMode, ref bool cancel, bool suspending)
        {
            if (rootFrame.Content != null)
            {
                IDictionary <string, object> stateDictionary = null;
                FrameworkElement             fe = rootFrame.Content as FrameworkElement;
                if (fe != null)
                {
                    if (fe.DataContext != null)
                    {
                        // Attempt to save the object off.
                        if (StateManager != null)
                        {
                            string key = GenerateStateKey(ViewModelKeyPrefix);
                            StateManager.SaveObject(key, fe.DataContext);
                            stateDictionary = StateManager.GetDictionary(key, true);
                        }

                        // See if the ViewModel participates in navigation-aware services.
                        INavigationAware navm = fe.DataContext as INavigationAware;
                        if (navm != null)
                        {
                            var e = (suspending)
                                        ? new NavigatingFromEventArgs(navMode, cancel, stateDictionary)
                                        : new NavigatingFromEventArgs(stateDictionary);
                            navm.OnNavigatingFrom(e);
                            cancel = e.Cancel;
                        }
                    }

                    // See if the VIEW participates in navigation-aware services.
                    INavigationAware naView = fe as INavigationAware;
                    if (naView != null)
                    {
                        // Save off the VIEW
                        stateDictionary = null;
                        if (StateManager != null)
                        {
                            string key = GenerateStateKey(PageKeyPrefix);
                            stateDictionary = StateManager.GetDictionary(key, false);
                        }

                        var e = (suspending)
                                    ? new NavigatingFromEventArgs(navMode, cancel, stateDictionary)
                                    : new NavigatingFromEventArgs(stateDictionary);
                        naView.OnNavigatingFrom(e);
                        cancel = e.Cancel;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void NotifyOnNavigatingFrom()
        {
            FrameworkElement contentElement = frame.Content as FrameworkElement;

            if (contentElement == null)
            {
                return;
            }

            INavigationAware navigationAwareContext = contentElement.DataContext as INavigationAware;

            if (navigationAwareContext != null)
            {
                navigationAwareContext.OnNavigatingFrom();
            }
        }