App.WP8.xaml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Diagnostics;
  3. using System.Resources;
  4. using System.Windows;
  5. using System.Windows.Markup;
  6. using System.Windows.Navigation;
  7. using Microsoft.Phone.Controls;
  8. using Microsoft.Phone.Shell;
  9. namespace Samples.Animation
  10. {
  11. public partial class App : Application
  12. {
  13. /// <summary>
  14. /// Provides easy access to the root frame of the Phone Application.
  15. /// </summary>
  16. /// <returns>The root frame of the Phone Application.</returns>
  17. public static PhoneApplicationFrame RootFrame { get; private set; }
  18. /// <summary>
  19. /// Constructor for the Application object.
  20. /// </summary>
  21. public App()
  22. {
  23. // Global handler for uncaught exceptions.
  24. UnhandledException += Application_UnhandledException;
  25. // Standard XAML initialization
  26. InitializeComponent();
  27. // Phone-specific initialization
  28. InitializePhoneApplication();
  29. // Show graphics profiling information while debugging.
  30. if (Debugger.IsAttached)
  31. {
  32. // Display the current frame rate counters.
  33. Application.Current.Host.Settings.EnableFrameRateCounter = true;
  34. // Show the areas of the app that are being redrawn in each frame.
  35. //Application.Current.Host.Settings.EnableRedrawRegions = true;
  36. // Enable non-production analysis visualization mode,
  37. // which shows areas of a page that are handed off to GPU with a colored overlay.
  38. //Application.Current.Host.Settings.EnableCacheVisualization = true;
  39. // Prevent the screen from turning off while under the debugger by disabling
  40. // the application's idle detection.
  41. // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
  42. // and consume battery power when the user is not using the phone.
  43. PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
  44. }
  45. }
  46. // Code to execute when the application is launching (eg, from Start)
  47. // This code will not execute when the application is reactivated
  48. private void Application_Launching(object sender, LaunchingEventArgs e)
  49. {
  50. }
  51. // Code to execute when the application is activated (brought to foreground)
  52. // This code will not execute when the application is first launched
  53. private void Application_Activated(object sender, ActivatedEventArgs e)
  54. {
  55. }
  56. // Code to execute when the application is deactivated (sent to background)
  57. // This code will not execute when the application is closing
  58. private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  59. {
  60. }
  61. // Code to execute when the application is closing (eg, user hit Back)
  62. // This code will not execute when the application is deactivated
  63. private void Application_Closing(object sender, ClosingEventArgs e)
  64. {
  65. }
  66. // Code to execute if a navigation fails
  67. private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
  68. {
  69. if (Debugger.IsAttached)
  70. {
  71. // A navigation has failed; break into the debugger
  72. Debugger.Break();
  73. }
  74. }
  75. // Code to execute on Unhandled Exceptions
  76. private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  77. {
  78. if (Debugger.IsAttached)
  79. {
  80. // An unhandled exception has occurred; break into the debugger
  81. Debugger.Break();
  82. }
  83. }
  84. #region Phone application initialization
  85. // Avoid double-initialization
  86. private bool phoneApplicationInitialized = false;
  87. // Do not add any additional code to this method
  88. private void InitializePhoneApplication()
  89. {
  90. if (phoneApplicationInitialized)
  91. return;
  92. // Create the frame but don't set it as RootVisual yet; this allows the splash
  93. // screen to remain active until the application is ready to render.
  94. RootFrame = new PhoneApplicationFrame();
  95. RootFrame.Navigated += CompleteInitializePhoneApplication;
  96. // Handle navigation failures
  97. RootFrame.NavigationFailed += RootFrame_NavigationFailed;
  98. // Handle reset requests for clearing the backstack
  99. RootFrame.Navigated += CheckForResetNavigation;
  100. // Ensure we don't initialize again
  101. phoneApplicationInitialized = true;
  102. }
  103. // Do not add any additional code to this method
  104. private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
  105. {
  106. // Set the root visual to allow the application to render
  107. if (RootVisual != RootFrame)
  108. RootVisual = RootFrame;
  109. // Remove this handler since it is no longer needed
  110. RootFrame.Navigated -= CompleteInitializePhoneApplication;
  111. }
  112. private void CheckForResetNavigation(object sender, NavigationEventArgs e)
  113. {
  114. // If the app has received a 'reset' navigation, then we need to check
  115. // on the next navigation to see if the page stack should be reset
  116. if (e.NavigationMode == NavigationMode.Reset)
  117. RootFrame.Navigated += ClearBackStackAfterReset;
  118. }
  119. private void ClearBackStackAfterReset(object sender, NavigationEventArgs e)
  120. {
  121. // Unregister the event so it doesn't get called again
  122. RootFrame.Navigated -= ClearBackStackAfterReset;
  123. // Only clear the stack for 'new' (forward) and 'refresh' navigations
  124. if (e.NavigationMode != NavigationMode.New && e.NavigationMode != NavigationMode.Refresh)
  125. return;
  126. // For UI consistency, clear the entire page stack
  127. while (RootFrame.RemoveBackEntry() != null)
  128. {
  129. ; // do nothing
  130. }
  131. }
  132. #endregion
  133. }
  134. }