BaconographyWP8.MainPage.OnNavigatedTo C# (CSharp) Method

OnNavigatedTo() protected method

protected OnNavigatedTo ( System.Windows.Navigation.NavigationEventArgs e ) : void
e System.Windows.Navigation.NavigationEventArgs
return void
		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			this.AdjustForOrientation(this.Orientation);

			if (e.NavigationMode == NavigationMode.Back)
			{
                if (pivot != null && pivot.Items != null &&  pivot.Items.FirstOrDefault() != null)
                {
                    var pivotItem = pivot.Items.FirstOrDefault() as PivotItem;
                    if (pivotItem != null && pivotItem.DataContext is RedditViewModel)
                    {
                        var redditViewModel = pivotItem.DataContext as RedditViewModel;
                        if (pivotItem.Content is RedditView)
                        {
                            var redditView = pivotItem.Content as RedditView;
                            redditView.LoadWithScroll();
                        }
                    }
                }
			}
            else if (e.NavigationMode == NavigationMode.Refresh)
            {

            }
            else if (e.NavigationMode == NavigationMode.New)
            {
                if (this.NavigationContext.QueryString.ContainsKey("data") && !string.IsNullOrWhiteSpace(this.NavigationContext.QueryString["data"]))
                {
                    try
                    {
                        //this appears to be a bug in WP8, the page is getting lazily bound but
                        //we're at a point where it should be completed
                        if (pivot.DataContext != null && pivot.ItemsSource == null)
                        {
                            pivot.ItemsSource = new ReifiedSubredditTemplateCollectionConverter().Convert(((MainPageViewModel)pivot.DataContext).PivotItems, null, null, null) as System.Collections.IEnumerable;
                        }
                        var unescapedData = Uri.UnescapeDataString(this.NavigationContext.QueryString["data"]);
                        var deserializedObject = JsonConvert.DeserializeObject<SelectTemporaryRedditMessage>(unescapedData);
                        if (deserializedObject is SelectTemporaryRedditMessage)
                        {
                            Messenger.Default.Send<SelectTemporaryRedditMessage>(deserializedObject as SelectTemporaryRedditMessage);
                            int indexToPosition;
                            if (pivot.DataContext != null && (((MainPageViewModel)pivot.DataContext).FindSubredditMessageIndex(deserializedObject as SelectTemporaryRedditMessage, out indexToPosition)))
                            {
                                pivot.SelectedIndex = indexToPosition;
                            }
                        }
                    }
                    catch (UriFormatException)
                    {
                        ServiceLocator.Current.GetInstance<IBaconProvider>().GetService<INotificationService>().CreateNotification("Invalid main page uri state, please PM /u/hippiehunter with details");
                    }
                }
                else if (this.NavigationContext.QueryString.ContainsKey("WallpaperSettings"))
                {
                    _navigationService.Navigate<SettingsPageView>("lockscreen");
                    while (NavigationService.BackStack.Count() > 0)
                        NavigationService.RemoveBackEntry();
                    return;
                }
            }

            _viewModelContextService.PushViewModelContext(DataContext as ViewModelBase);
            _smartOfflineService.NavigatedToView(typeof(MainPage), (e.NavigationMode == NavigationMode.New));
		}