PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/DataBoundApp1/DetailsPage.xaml.cs

https://bitbucket.org/JeffF/wp7codemashsmackdown2012
C# | 57 lines | 47 code | 8 blank | 2 comment | 7 complexity | 617463941658472fbc396b1b878a6134 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Navigation;
  5. using DataBoundApp1.ViewModels;
  6. using Microsoft.Phone.Controls;
  7. namespace DataBoundApp1
  8. {
  9. public partial class DetailsPage : PhoneApplicationPage
  10. {
  11. // Constructor
  12. public DetailsPage()
  13. {
  14. InitializeComponent();
  15. }
  16. // When page is navigated to set data context to selected item in list
  17. protected override void OnNavigatedTo(NavigationEventArgs e)
  18. {
  19. string selectedIndex = "";
  20. if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
  21. {
  22. DataContext = App.ViewModel.Sessions.SingleOrDefault(s => s.Uri == selectedIndex);
  23. }
  24. }
  25. private SessionViewModel Session
  26. {
  27. get { return (SessionViewModel) this.DataContext; }
  28. }
  29. private void ToggleFavorite(object sender, EventArgs e)
  30. {
  31. if (App.ViewModel.Favorites.Any(s => s.Start == Session.Start))
  32. {
  33. MessageBox.Show("You've alread added the session for this slot. Don't do that.");
  34. return;
  35. }
  36. var foundSession = App.ViewModel.Favorites.SingleOrDefault(s => s.Uri == Session.Uri);
  37. if (foundSession != null)
  38. {
  39. App.ViewModel.Favorites.Remove(foundSession);
  40. MessageBox.Show("session removed.");
  41. }
  42. else
  43. {
  44. App.ViewModel.Favorites.Add(Session);
  45. MessageBox.Show("session added.");
  46. }
  47. }
  48. }
  49. }