App.xaml.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Copyright (c) 2012 Nokia Corporation.
  3. */
  4. using System;
  5. using System.Diagnostics;
  6. using System.Resources;
  7. using System.Windows;
  8. using System.Windows.Markup;
  9. using System.Windows.Navigation;
  10. using Microsoft.Phone.Controls;
  11. using Microsoft.Phone.Shell;
  12. using NfcNdefTagReader.Resources;
  13. namespace NfcNdefTagReader
  14. {
  15. public partial class App : Application
  16. {
  17. /// <summary>
  18. /// Provides easy access to the root frame of the Phone Application.
  19. /// </summary>
  20. /// <returns>The root frame of the Phone Application.</returns>
  21. public static PhoneApplicationFrame RootFrame { get; private set; }
  22. /// <summary>
  23. /// Constructor for the Application object.
  24. /// </summary>
  25. public App()
  26. {
  27. // Global handler for uncaught exceptions.
  28. UnhandledException += Application_UnhandledException;
  29. // Standard XAML initialization
  30. InitializeComponent();
  31. // Phone-specific initialization
  32. InitializePhoneApplication();
  33. // Language display initialization
  34. InitializeLanguage();
  35. // Show graphics profiling information while debugging.
  36. if (Debugger.IsAttached)
  37. {
  38. // Display the current frame rate counters.
  39. Application.Current.Host.Settings.EnableFrameRateCounter = true;
  40. // Show the areas of the app that are being redrawn in each frame.
  41. //Application.Current.Host.Settings.EnableRedrawRegions = true;
  42. // Enable non-production analysis visualization mode,
  43. // which shows areas of a page that are handed off to GPU with a colored overlay.
  44. //Application.Current.Host.Settings.EnableCacheVisualization = true;
  45. // Prevent the screen from turning off while under the debugger by disabling
  46. // the application's idle detection.
  47. // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
  48. // and consume battery power when the user is not using the phone.
  49. PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
  50. }
  51. }
  52. // Code to execute when the application is launching (eg, from Start)
  53. // This code will not execute when the application is reactivated
  54. private void Application_Launching(object sender, LaunchingEventArgs e)
  55. {
  56. }
  57. // Code to execute when the application is activated (brought to foreground)
  58. // This code will not execute when the application is first launched
  59. private void Application_Activated(object sender, ActivatedEventArgs e)
  60. {
  61. }
  62. // Code to execute when the application is deactivated (sent to background)
  63. // This code will not execute when the application is closing
  64. private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  65. {
  66. }
  67. // Code to execute when the application is closing (eg, user hit Back)
  68. // This code will not execute when the application is deactivated
  69. private void Application_Closing(object sender, ClosingEventArgs e)
  70. {
  71. }
  72. // Code to execute if a navigation fails
  73. private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
  74. {
  75. if (Debugger.IsAttached)
  76. {
  77. // A navigation has failed; break into the debugger
  78. Debugger.Break();
  79. }
  80. }
  81. // Code to execute on Unhandled Exceptions
  82. private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  83. {
  84. if (Debugger.IsAttached)
  85. {
  86. // An unhandled exception has occurred; break into the debugger
  87. Debugger.Break();
  88. }
  89. }
  90. #region Phone application initialization
  91. // Avoid double-initialization
  92. private bool phoneApplicationInitialized = false;
  93. // Do not add any additional code to this method
  94. private void InitializePhoneApplication()
  95. {
  96. if (phoneApplicationInitialized)
  97. return;
  98. // Create the frame but don't set it as RootVisual yet; this allows the splash
  99. // screen to remain active until the application is ready to render.
  100. RootFrame = new PhoneApplicationFrame();
  101. RootFrame.Navigated += CompleteInitializePhoneApplication;
  102. // Handle navigation failures
  103. RootFrame.NavigationFailed += RootFrame_NavigationFailed;
  104. // Handle reset requests for clearing the backstack
  105. RootFrame.Navigated += CheckForResetNavigation;
  106. // Ensure we don't initialize again
  107. phoneApplicationInitialized = true;
  108. }
  109. // Do not add any additional code to this method
  110. private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
  111. {
  112. // Set the root visual to allow the application to render
  113. if (RootVisual != RootFrame)
  114. RootVisual = RootFrame;
  115. // Remove this handler since it is no longer needed
  116. RootFrame.Navigated -= CompleteInitializePhoneApplication;
  117. }
  118. private void CheckForResetNavigation(object sender, NavigationEventArgs e)
  119. {
  120. // If the app has received a 'reset' navigation, then we need to check
  121. // on the next navigation to see if the page stack should be reset
  122. if (e.NavigationMode == NavigationMode.Reset)
  123. RootFrame.Navigated += ClearBackStackAfterReset;
  124. }
  125. private void ClearBackStackAfterReset(object sender, NavigationEventArgs e)
  126. {
  127. // Unregister the event so it doesn't get called again
  128. RootFrame.Navigated -= ClearBackStackAfterReset;
  129. // Only clear the stack for 'new' (forward) and 'refresh' navigations
  130. if (e.NavigationMode != NavigationMode.New && e.NavigationMode != NavigationMode.Refresh)
  131. return;
  132. // For UI consistency, clear the entire page stack
  133. while (RootFrame.RemoveBackEntry() != null)
  134. {
  135. ; // do nothing
  136. }
  137. }
  138. #endregion
  139. // Initialize the app's font and flow direction as defined in its localized resource strings.
  140. //
  141. // To ensure that the font of your application is aligned with its supported languages and that the
  142. // FlowDirection for each of those languages follows its traditional direction, ResourceLanguage
  143. // and ResourceFlowDirection should be initialized in each resx file to match these values with that
  144. // file's culture. For example:
  145. //
  146. // AppResources.es-ES.resx
  147. // ResourceLanguage's value should be "es-ES"
  148. // ResourceFlowDirection's value should be "LeftToRight"
  149. //
  150. // AppResources.ar-SA.resx
  151. // ResourceLanguage's value should be "ar-SA"
  152. // ResourceFlowDirection's value should be "RightToLeft"
  153. //
  154. // For more info on localizing Windows Phone apps see http://go.microsoft.com/fwlink/?LinkId=262072.
  155. //
  156. private void InitializeLanguage()
  157. {
  158. try
  159. {
  160. // Set the font to match the display language defined by the
  161. // ResourceLanguage resource string for each supported language.
  162. //
  163. // Fall back to the font of the neutral language if the Display
  164. // language of the phone is not supported.
  165. //
  166. // If a compiler error is hit then ResourceLanguage is missing from
  167. // the resource file.
  168. RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);
  169. // Set the FlowDirection of all elements under the root frame based
  170. // on the ResourceFlowDirection resource string for each
  171. // supported language.
  172. //
  173. // If a compiler error is hit then ResourceFlowDirection is missing from
  174. // the resource file.
  175. FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);
  176. RootFrame.FlowDirection = flow;
  177. }
  178. catch
  179. {
  180. // If an exception is caught here it is most likely due to either
  181. // ResourceLangauge not being correctly set to a supported language
  182. // code or ResourceFlowDirection is set to a value other than LeftToRight
  183. // or RightToLeft.
  184. if (Debugger.IsAttached)
  185. {
  186. Debugger.Break();
  187. }
  188. throw;
  189. }
  190. }
  191. }
  192. }