/// <summary> /// Checks if user preferences already exist. /// If so, redirect user to the next page. /// If not, initialize starting page /// </summary> /// <param name="e"></param> protected async override void OnNavigatedTo(NavigationEventArgs e) { if (isFirstNavigation) { if (IsolatedStorageSettings.ApplicationSettings.Contains("preferences")) { string[] preferences = JsonConvert.DeserializeObject<string[]> (IsolatedStorageSettings.ApplicationSettings["preferences"].ToString()); NavigationService.Navigate(new Uri(string.Format("/CountrySelect.xaml?countryId={0}&role={1}&prog={2}", preferences[0], preferences[1], preferences[2]), UriKind.Relative)); return; } Country = new CountryModel() { Countries = await App.MobileService.GetTable<CountryData>().ToListAsync() }; foreach (CountryData data in Country.Countries) { data.FlagImage = ImageConversionHelper.ToImage(data.Flag); data.Flag = String.Empty; } DataContext = Country; isFirstNavigation = false; } }
/// <summary> /// Checks if 'search' parameter exists /// If parameter does exists, get the filtered results and pass it to DataContext /// If parameter does not exists, do nothing /// </summary> /// <param name="e"></param> /// <summary> protected async override void OnNavigatedTo(NavigationEventArgs e) { listBox.Opacity = 0; //If this is initial call to the event if (isFirstNavigation) { SystemTray.ProgressIndicator = new ProgressIndicator(); ProgressIndicatorHelper.SetProgressBar(true, AppResources.ProgressIndicatorCountries); //Get index of previously selected country int selectedCountryIndex = Int32.Parse(IsolatedStorageSettings. ApplicationSettings["selectedCountryIndex"].ToString()); //Populate CountryModel with every CountryData that satisfies parameters model = new CountryModel() { Countries = await App.MobileService.GetTable<CountryData>(). Where(x => x.Id != selectedCountryIndex). ToListAsync() }; Random rand = new Random(); //Convert Flag to FlagImage //After conversion, empty Flag property foreach (CountryData data in model.Countries) { if(data.Rating == 0.0) data.Rating = rand.Next(0, 5); data.FlagImage = ImageConversionHelper.ToImage(data.Flag); data.Flag = String.Empty; } //initialize double array for country coordinates countryCoordinates = new double[2]; ProgressIndicatorHelper.SetProgressBar(false, null); isFirstNavigation = false; } textBoxSearch.Text = String.Empty; textBoxSearch.Visibility = System.Windows.Visibility.Collapsed; //If user has entered a search term if (NavigationContext.QueryString.ContainsKey("search")) { sortCounter = 0; string searchTerm = NavigationContext.QueryString["search"]; //Populate CountryModel with data that satisfies search term //Always search entire model, not DataContext CountryModel filteredModel = new CountryModel() { Countries = model.Countries.Where(x => x.Name.Contains(searchTerm)).ToList() }; //filteredModel.Countries = model.Countries.Where(x => x.Name.Contains(searchTerm)).ToList(); DataContext = filteredModel; //Preserve map visibility across navigation/refresh if (isMapVisible) map.Visibility = System.Windows.Visibility.Visible; } else DataContext = model; //Animate listBox with countries AnimationHelper.Fade(listBox, 1, 700, new PropertyPath(OpacityProperty)); }
/// <summary> /// Sorts the country list by highest rating, and alphabetically (both ascending and descending) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void sortIconButton_Click(object sender, EventArgs e) { if (sortCounter == 0) DataContext = new CountryModel() { Countries = (DataContext as CountryModel). Countries.OrderByDescending(x => x.Rating).ToList() }; else if (sortCounter == 1) DataContext = new CountryModel() { Countries = (DataContext as CountryModel). Countries.OrderByDescending(x => x.Name).ToList() }; else DataContext = new CountryModel() { Countries = (DataContext as CountryModel). Countries.OrderBy(x => x.Name).ToList() }; sortCounter += 1; if (sortCounter == 3) sortCounter = 0; }