/// <summary>
 /// Post Navigation event initialization for the page.
 /// This should be called if we are constructing the page.
 /// </summary>
 private void PostNavigationToInitialize()
 {
     CategorySelectionViewModel.InitializeCategoryBinding(this);
     // Show the pivot control, since we hid it until we go the correct page
     pivot.Opacity = 1;
     this.viewModel.UpdateFavoriteList(this.favoritesListView, this.favoritesNoItems);
 }
 /// <summary>
 /// Pivot control has been loaded
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">Event data.</param>
 private void OnPivotLoaded(object sender, RoutedEventArgs e)
 {
     Dispatcher.BeginInvoke(() =>
     {
         this.pivot.Loaded -= new RoutedEventHandler(OnPivotLoaded);
         // We don't want multiple calls to the Selection Changed event, so we enable it now
         this.pivot.SelectionChanged += new SelectionChangedEventHandler(OnPivotSelectionChanged);
         // Set Pivot item index now to avoid known issue in pivot control
         this.viewModel.SelectedDataIndex = this.requestedPivotDataSelectionIndex;
         CategorySelectionViewModel.RestoreUserSelections();
     }
                            );
 }
        /// <summary>
        /// Called when we have a page to navigated event
        /// This event is raised whenever we visit the page. This can occur for the
        /// following situations
        /// 1) Application Activation where we are tomb stoned, or we are still in memory
        ///    If we have been tomb stoned, we need to read objects from the application
        ///    service, and then perform any initialization on the restored objects.
        ///    If we received and activation event, but we were not tomb stoned, then
        ///    we actually don't need to do anything except clear the main application
        ///    state flag and exit. This is because all objects are still in memory.
        /// 2) We have come from the main page of the application.
        /// To work around a known issue in the Pivot control related to applying templates to
        /// non default pivot items we can't set the Pivot Index in this function.
        /// We therefore save the desired pivot index and we will set this later
        /// </summary>
        /// <param name="e">Event data.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (ApplicationState.ApplicationStartup == AppOpenState.None &&
                this.pivot.Items.Count <= 1)
            {
                // We have come from the main page of the application
                this.viewModel.AllowNavigation = false;
                this.PostNavigationToInitialize();
                CategorySelectionViewModel.SyncStateToMainPageState();
                this.requestedPivotDataSelectionIndex =
                    CategorySelectionViewModel.GetCategoryPivotIndex(pivot);
            }
            else
            {
                // Activated event case
                if (!this.IsPageActivated)
                {
                    // We are returning to the application, but we were not tomb stoned.
                    ApplicationState.ApplicationStartup = AppOpenState.None;
                    return;
                }
                this.viewModel.AllowNavigation = false;
                ApplicationState.RetrieveAppObjects(true);
                this.PostNavigationToInitialize();
                this.requestedPivotDataSelectionIndex =
                    ApplicationState.CategoryPageInformation.PivotSelectedIndex;
                if (this.requestedPivotDataSelectionIndex == 0)
                {
                    /* If the index is 0, we don't receive an event for the pivot item
                     * changing which will cause the pivot to not render correctly.
                     * We can set the index to 1 ( normal default) without causing any
                     * problem for the pivot control. We will then set the index to
                     * 0 later and we should get the event to fire
                     */
                    this.viewModel.SelectedDataIndex = 1;
                }
            }

            // Reenabled buttons
            this.viewModel.AllowNavigation = true;
            pivot.Opacity = 0;
            ApplicationState.ApplicationStartup = AppOpenState.None;
        }