/// <summary> /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// </summary> /// <param name="navigationParameter">The parameter value passed to /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested. /// </param> /// <param name="pageState">A dictionary of state preserved by this page during an earlier /// session. This will be null the first time a page is visited.</param> protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) { string selectedHeroJson = (string)navigationParameter; SelectedHero selectedHero = JsonConvert.DeserializeObject<SelectedHero>(selectedHeroJson); _profile = selectedHero.Profile; _heroes = new Hero[_profile.Heroes.Count]; _d3Client = new D3Client(_profile.Region); _compareStats = new ObservableCollection<CompareStat>(); compareStatsItemsViewSource.Source = _compareStats; try { StorageFile sampleFile = await ApplicationData.Current.LocalFolder.GetFileAsync(_profile.BattleTag.Replace("#", "-") + ".txt"); string heroes = await FileIO.ReadTextAsync(sampleFile); if (!String.IsNullOrEmpty(heroes)) { _heroes = JsonConvert.DeserializeObject<Hero[]>(heroes); } } catch (FileNotFoundException) { } // File doesn't exist. this.DefaultViewModel["Group"] = _profile; this.DefaultViewModel["Heroes"] = _profile.Heroes; if (pageState == null) { // When this is a new page, select the first item automatically unless logical page // navigation is being used (see the logical page navigation #region below.) if (!this.UsingLogicalPageNavigation() && this.itemsViewSource.View != null) { if (_isFirstLoad && itemListView.SelectedIndex == selectedHero.HeroIndex) LoadHero(itemListView.Items[0] as ProfileHero, 0, false); _isFirstLoad = false; itemListView.SelectedIndex = selectedHero.HeroIndex; } } else { // Restore the previously saved state associated with this page if (pageState.ContainsKey("SelectedHero") && this.itemsViewSource.View != null) { _isFirstLoad = false; itemListView.SelectedIndex = (int)pageState["SelectedHero"]; } } }
private async void RefreshProfile(string battleTag, Region region) { _d3Client = new D3Client(region); battleTag = battleTag.Replace("#", "-"); Profile profile = await _d3Client.GetProfileAsync(battleTag); if (profile == null || profile.Heroes == null) { var messageDialog = new Windows.UI.Popups.MessageDialog( String.Format("The profile: {0} on region {1} is not valid.", new object[] { battleTag.Replace("-","#"), region }), "Profile doesn't exist"); await messageDialog.ShowAsync(); } else { profile.Region = region; // Set the Profile iamge. string imageName = String.Empty; int x; int y; int backx = 524; int backy = 0; foreach(ProfileHero profileHero in profile.Heroes) { GetXYFromClassGender(profileHero.Class, profileHero.Gender, out x, out y); if (profileHero.Hardcore) backy = 205; else backy = 0; if (profileHero.Id == profile.LastHeroPlayed) { profile.ProfilePortrait = new Portrait(); profile.ProfilePortrait.ViewRect = String.Format("{0},{1},168,130", new object[] {x, y}); profile.ProfilePortrait.Margin = String.Format("{0},{1},0,0", new object[] { x*-1, y*-1 }); profile.ProfilePortrait.BackgroundViewRect = String.Format("{0},{1},193,205", new object[] { backx, backy }); profile.ProfilePortrait.BackgroundMargin = String.Format("{0},{1},0,0", new object[] { backx * -1, backy * -1 }); } profileHero.Portrait = new Portrait(); profileHero.Portrait.ViewRect = String.Format("{0},{1},168,130", new object[] { x, y }); profileHero.Portrait.Margin = String.Format("{0},{1},0,0", new object[] { x * -1, y * -1 }); profileHero.Portrait.BackgroundViewRect = String.Format("{0},{1},193,205", new object[] { backx, backy }); profileHero.Portrait.BackgroundMargin = String.Format("{0},{1},0,0", new object[] { backx * -1, backy * -1 }); } profile.HeroHelperLastUpdated = DateTime.Now; // If the profile already exists in the list, remove it and add the new one. int index = 0; bool found = false; for (int i = 0; i < _recentProfiles.Count; i++) { if (_recentProfiles[i].BattleTag.Equals(profile.BattleTag, StringComparison.CurrentCultureIgnoreCase)) { found = true; index = i; break; } } if (found) _recentProfiles.RemoveAt(index); _recentProfiles.Insert(0, profile); if (_recentProfiles.Count > _recentProfileCap) _recentProfiles.RemoveAt(_recentProfileCap); SaveRecentProfiles(); // Just in case heroes are added or removed since last refresh. DeleteProfileHeroesFile(battleTag); // Select the first item in the list. itemListView.SelectedIndex = 0; } }