MakeReservation.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright © 2011-2012 Nokia Corporation. All rights reserved.
  3. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
  4. * Other product and company names mentioned herein may be trademarks
  5. * or trade names of their respective owners.
  6. * See LICENSE.TXT for license information.
  7. */
  8. using System;
  9. using System.Linq;
  10. using System.Windows.Controls;
  11. using System.Windows.Navigation;
  12. using Microsoft.Phone.Controls;
  13. using Microsoft.Phone.Shell;
  14. namespace wprestaurantapp
  15. {
  16. /// <summary>
  17. /// Page for creating or modifying a reservation
  18. /// </summary>
  19. public partial class MakeReservation : PhoneApplicationPage
  20. {
  21. /// <summary>
  22. /// Constructor
  23. /// </summary>
  24. public MakeReservation()
  25. {
  26. InitializeComponent();
  27. // Set the data context
  28. DataContext = App.ViewModel;
  29. // Application bar is not a silverlight component -> it doesn't support data binding, which means
  30. // that it cannot be localized with XAML. Therefore, create it using C#
  31. ApplicationBar = new ApplicationBar();
  32. ApplicationBar.IsVisible = true;
  33. ApplicationBar.IsMenuEnabled = true;
  34. _button1 = new ApplicationBarIconButton(new Uri("/Toolkit.Content/ApplicationBar.Check.png", UriKind.Relative));
  35. _button1.Text = wprestaurantapp.AppResources.ButtonDone;
  36. _button1.IsEnabled = false;
  37. _button1.Click += new EventHandler(button1_click);
  38. ApplicationBarIconButton button2 = new ApplicationBarIconButton(new Uri("/Toolkit.Content/ApplicationBar.Cancel.png", UriKind.Relative));
  39. button2.Text = wprestaurantapp.AppResources.ButtonCancel;
  40. button2.IsEnabled = true;
  41. button2.Click += new EventHandler(button2_click);
  42. ApplicationBar.Buttons.Add(_button1);
  43. ApplicationBar.Buttons.Add(button2);
  44. }
  45. /// <summary>
  46. /// Overridden OnNavigatedTo handler
  47. /// </summary>
  48. protected override void OnNavigatedTo(NavigationEventArgs e)
  49. {
  50. base.OnNavigatedTo(e);
  51. // Make sure that data context is loaded
  52. if (!App.ViewModel.IsDataLoaded)
  53. {
  54. App.ViewModel.LoadData();
  55. }
  56. if (isNewInstance)
  57. {
  58. _reservationIndex = int.Parse(NavigationContext.QueryString["index"]);
  59. if (_reservationIndex != -1)
  60. {
  61. Reservation reservation = App.ViewModel.Restaurant.Reservations.ElementAt(_reservationIndex);
  62. NameInput.Text = reservation.Reserver;
  63. TelephoneInput.Text = reservation.Telephone;
  64. PersonsInput.Text = reservation.Persons.ToString();
  65. DatePicker.Value = new DateTime(reservation.Time.Year, reservation.Time.Month, reservation.Time.Day,
  66. reservation.Time.Hour, reservation.Time.Minute, reservation.Time.Second);
  67. TimePicker.Value = new DateTime(reservation.Time.Year, reservation.Time.Month, reservation.Time.Day,
  68. reservation.Time.Hour, reservation.Time.Minute, reservation.Time.Second);
  69. }
  70. // Restore transient page state in activation
  71. if (State.ContainsKey("Reserver"))
  72. {
  73. NameInput.Text = (string)State["Reserver"];
  74. }
  75. if (State.ContainsKey("Telephone"))
  76. {
  77. TelephoneInput.Text = (string)State["Telephone"];
  78. }
  79. if (State.ContainsKey("Persons"))
  80. {
  81. PersonsInput.Text = (string)State["Persons"];
  82. }
  83. if (State.ContainsKey("DatePicker"))
  84. {
  85. DatePicker.Value = (DateTime?)State["DatePicker"];
  86. }
  87. if (State.ContainsKey("TimePicker"))
  88. {
  89. TimePicker.Value = (DateTime?)State["TimePicker"];
  90. }
  91. if (State.ContainsKey("ReservationIndex"))
  92. {
  93. _reservationIndex = (int)State["ReservationIndex"];
  94. }
  95. isNewInstance = false;
  96. }
  97. }
  98. /// <summary>
  99. /// Overridden OnNavigatedFrom handler
  100. /// </summary>
  101. protected override void OnNavigatedFrom(NavigationEventArgs e)
  102. {
  103. base.OnNavigatedFrom(e);
  104. if (e.NavigationMode != NavigationMode.Back)
  105. {
  106. // Save transient page state when moving away
  107. State["Reserver"] = NameInput.Text;
  108. State["Telephone"] = TelephoneInput.Text;
  109. State["Persons"] = PersonsInput.Text;
  110. State["ReservationIndex"] = _reservationIndex;
  111. // When navigating to DatePicker/TimePicker OnNavigatedFrom() gets called. We should _not_ be saving the states
  112. // of DatePicker/TimePicker in that case, because we would end up overwriting the value(s) selected by the user
  113. // in OnNavigatedTo().
  114. // DatePicker and TimePicker are part of Silverlight Toolkit which does not ship as part of this
  115. // example. Please see release notes for instructions how to install and use Silverlight Toolkit.
  116. string uri = e.Uri.ToString();
  117. if (uri != "/Microsoft.Phone.Controls.Toolkit;component/DateTimePickers/DatePickerPage.xaml" &&
  118. uri != "/Microsoft.Phone.Controls.Toolkit;component/DateTimePickers/TimePickerPage.xaml")
  119. {
  120. State["DatePicker"] = DatePicker.Value;
  121. State["TimePicker"] = TimePicker.Value;
  122. }
  123. else
  124. {
  125. State.Remove("DatePicker");
  126. State.Remove("TimePicker");
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Handler to validate entered user input
  132. /// </summary>
  133. /// <param name="sender"></param>
  134. /// <param name="e"></param>
  135. private void validateFields(object sender, TextChangedEventArgs e)
  136. {
  137. bool name = ( NameInput.Text != "" );
  138. bool telephone = ( TelephoneInput.Text != "" );
  139. int temp = 0;
  140. bool persons = ( int.TryParse( PersonsInput.Text, out temp ) ) && ( temp > 0 );
  141. if( name && telephone && persons )
  142. {
  143. _button1.IsEnabled = true;
  144. }
  145. else
  146. {
  147. _button1.IsEnabled = false;
  148. }
  149. }
  150. /// <summary>
  151. /// Handler for Done button
  152. /// </summary>
  153. /// <param name="sender"></param>
  154. /// <param name="e"></param>
  155. private void button1_click(object sender, EventArgs e)
  156. {
  157. Reservation reservation = new Reservation();
  158. reservation.Reserver = NameInput.Text;
  159. reservation.Telephone = TelephoneInput.Text;
  160. reservation.Persons = int.Parse(PersonsInput.Text);
  161. DateTime date = DatePicker.Value ?? DateTime.Now;
  162. DateTime time = TimePicker.Value ?? DateTime.Now;
  163. reservation.Time = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second);
  164. if (_reservationIndex == -1)
  165. {
  166. App.ViewModel.Restaurant.Reservations.Add(reservation);
  167. }
  168. else
  169. {
  170. App.ViewModel.Restaurant.Reservations.RemoveAt(_reservationIndex);
  171. App.ViewModel.Restaurant.Reservations.Insert(_reservationIndex, reservation);
  172. }
  173. NavigationService.GoBack();
  174. }
  175. /// <summary>
  176. /// Handler for Cancel button
  177. /// </summary>
  178. /// <param name="sender"></param>
  179. /// <param name="e"></param>
  180. private void button2_click(object sender, EventArgs e)
  181. {
  182. NavigationService.GoBack();
  183. }
  184. /// <summary>
  185. /// True when this object instance has been just created, otherwise false
  186. /// </summary>
  187. private bool isNewInstance = true;
  188. /// <summary>
  189. /// Member variable for Done button
  190. /// </summary>
  191. private ApplicationBarIconButton _button1;
  192. /// <summary>
  193. /// Member variable storing index of the reservation. -1 if creating a new reservation, otherwise
  194. /// other than -1
  195. /// </summary>
  196. private int _reservationIndex;
  197. }
  198. }