App.xaml.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Windows;
  9. using System.Windows.Navigation;
  10. using Microsoft.Phone.Controls;
  11. using Microsoft.Phone.Shell;
  12. namespace wprestaurantapp
  13. {
  14. /// <summary>
  15. /// Application instance
  16. /// </summary>
  17. public partial class App : Application
  18. {
  19. /// <summary>
  20. /// Class variable for ViewModel property
  21. /// </summary>
  22. private static MainViewModel viewModel = null;
  23. /// <summary>
  24. /// A static ViewModel used by the views to bind against.
  25. /// </summary>
  26. /// <returns>The MainViewModel object.</returns>
  27. public static MainViewModel ViewModel
  28. {
  29. get
  30. {
  31. // Delay creation of the view model until necessary
  32. if (viewModel == null)
  33. viewModel = new MainViewModel();
  34. return viewModel;
  35. }
  36. }
  37. /// <summary>
  38. /// Provides easy access to the root frame of the Phone Application.
  39. /// </summary>
  40. /// <returns>The root frame of the Phone Application.</returns>
  41. public PhoneApplicationFrame RootFrame { get; private set; }
  42. /// <summary>
  43. /// Constructor for the Application object.
  44. /// </summary>
  45. public App()
  46. {
  47. // Global handler for uncaught exceptions.
  48. UnhandledException += Application_UnhandledException;
  49. // Show graphics profiling information while debugging.
  50. if (System.Diagnostics.Debugger.IsAttached)
  51. {
  52. // Display the current frame rate counters.
  53. //Application.Current.Host.Settings.EnableFrameRateCounter = true;
  54. // Show the areas of the app that are being redrawn in each frame.
  55. //Application.Current.Host.Settings.EnableRedrawRegions = true;
  56. // Enable non-production analysis visualization mode,
  57. // which shows areas of a page that are being GPU accelerated with a colored overlay.
  58. //Application.Current.Host.Settings.EnableCacheVisualization = true;
  59. }
  60. // Standard Silverlight initialization
  61. InitializeComponent();
  62. // Phone-specific initialization
  63. InitializePhoneApplication();
  64. }
  65. // Code to execute when the application is launching (eg, from Start)
  66. // This code will not execute when the application is reactivated
  67. private void Application_Launching(object sender, LaunchingEventArgs e)
  68. {
  69. }
  70. // Code to execute when the application is activated (brought to foreground)
  71. // This code will not execute when the application is first launched
  72. private void Application_Activated(object sender, ActivatedEventArgs e)
  73. {
  74. }
  75. // Code to execute when the application is deactivated (sent to background)
  76. // This code will not execute when the application is closing
  77. private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  78. {
  79. ViewModel.SaveData();
  80. }
  81. // Code to execute when the application is closing (eg, user hit Back)
  82. // This code will not execute when the application is deactivated
  83. private void Application_Closing(object sender, ClosingEventArgs e)
  84. {
  85. ViewModel.SaveData();
  86. }
  87. // Code to execute if a navigation fails
  88. private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
  89. {
  90. if (System.Diagnostics.Debugger.IsAttached)
  91. {
  92. // A navigation has failed; break into the debugger
  93. System.Diagnostics.Debugger.Break();
  94. }
  95. }
  96. // Code to execute on Unhandled Exceptions
  97. private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  98. {
  99. if (System.Diagnostics.Debugger.IsAttached)
  100. {
  101. // An unhandled exception has occurred; break into the debugger
  102. System.Diagnostics.Debugger.Break();
  103. }
  104. }
  105. #region Phone application initialization
  106. // Avoid double-initialization
  107. private bool phoneApplicationInitialized = false;
  108. // Do not add any additional code to this method
  109. private void InitializePhoneApplication()
  110. {
  111. if (phoneApplicationInitialized)
  112. return;
  113. // Create the frame but don't set it as RootVisual yet; this allows the splash
  114. // screen to remain active until the application is ready to render.
  115. RootFrame = new PhoneApplicationFrame();
  116. RootFrame.Navigated += CompleteInitializePhoneApplication;
  117. // Handle navigation failures
  118. RootFrame.NavigationFailed += RootFrame_NavigationFailed;
  119. // Ensure we don't initialize again
  120. phoneApplicationInitialized = true;
  121. }
  122. // Do not add any additional code to this method
  123. private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
  124. {
  125. // Set the root visual to allow the application to render
  126. if (RootVisual != RootFrame)
  127. RootVisual = RootFrame;
  128. // Remove this handler since it is no longer needed
  129. RootFrame.Navigated -= CompleteInitializePhoneApplication;
  130. }
  131. #endregion
  132. }
  133. }