PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Test/WP8Test/App.xaml.cs

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