App.xaml.cs 8.8 KB

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