App.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // ----------------------------------------------------------------------------------
  2. // Microsoft Developer & Platform Evangelism
  3. //
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. // OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. // ----------------------------------------------------------------------------------
  10. // The example companies, organizations, products, domain names,
  11. // e-mail addresses, logos, people, places, and events depicted
  12. // herein are fictitious. No association with any real company,
  13. // organization, product, domain name, email address, logo, person,
  14. // places, or events is intended or should be inferred.
  15. // ----------------------------------------------------------------------------------
  16. using System.Windows;
  17. using System.Windows.Navigation;
  18. using Microsoft.Phone.Controls;
  19. using Microsoft.Phone.Shell;
  20. namespace WindowsPhoneRecipes
  21. {
  22. public partial class App : Application
  23. {
  24. // Easy access to the root frame
  25. public PhoneApplicationFrame RootFrame { get; private set; }
  26. // Constructor
  27. public App()
  28. {
  29. // Global handler for uncaught exceptions.
  30. // Note that exceptions thrown by ApplicationBarItem.Click will not get caught here.
  31. UnhandledException += Application_UnhandledException;
  32. // Standard Silverlight initialization
  33. InitializeComponent();
  34. // Phone-specific initialization
  35. InitializePhoneApplication();
  36. WindowsPhoneRecipes.Logger.Instance.AddLine();
  37. ///////////////////////////////////////////////////////////////////////////////////
  38. // Init the NonLinearNavigationService, make sure you call it last in the app CTOR
  39. // (this has minimal impact on load time)
  40. ///////////////////////////////////////////////////////////////////////////////////
  41. NonLinearNavigationService.Instance.Initialize(RootFrame);
  42. }
  43. // Code to execute when the application is launching (eg, from Start)
  44. // This code will not execute when the application is reactivated
  45. private void Application_Launching(object sender, LaunchingEventArgs e)
  46. {
  47. WindowsPhoneRecipes.Logger.Instance.AddLine();
  48. }
  49. // Code to execute when the application is activated (brought to foreground)
  50. // This code will not execute when the application is first launched
  51. private void Application_Activated(object sender, ActivatedEventArgs e)
  52. {
  53. WindowsPhoneRecipes.Logger.Instance.AddLine();
  54. }
  55. // Code to execute when the application is deactivated (sent to background)
  56. // This code will not execute when the application is closing
  57. private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  58. {
  59. WindowsPhoneRecipes.Logger.Instance.AddLine();
  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. WindowsPhoneRecipes.Logger.Instance.AddLine();
  66. }
  67. // Code to execute if a navigation fails
  68. void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
  69. {
  70. if (System.Diagnostics.Debugger.IsAttached)
  71. {
  72. // A navigation has failed; break into the debugger
  73. System.Diagnostics.Debugger.Break();
  74. }
  75. }
  76. // Code to execute on Unhandled Exceptions
  77. private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  78. {
  79. if (System.Diagnostics.Debugger.IsAttached)
  80. {
  81. // An unhandled exception has occurred; break into the debugger
  82. System.Diagnostics.Debugger.Break();
  83. }
  84. }
  85. #region Phone application initialization
  86. // Avoid double-initialization
  87. private bool phoneApplicationInitialized = false;
  88. // Do not add any additional code to this method
  89. private void InitializePhoneApplication()
  90. {
  91. if (phoneApplicationInitialized)
  92. return;
  93. // Create the frame but don't set it as RootVisual yet; this allows the splash
  94. // screen to remain active until the application is ready to render.
  95. RootFrame = new PhoneApplicationFrame();
  96. RootFrame.Navigated += CompleteInitializePhoneApplication;
  97. // Handle navigation failures
  98. RootFrame.NavigationFailed += RootFrame_NavigationFailed;
  99. // Ensure we don't initialize again
  100. phoneApplicationInitialized = true;
  101. }
  102. // Do not add any additional code to this method
  103. private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
  104. {
  105. // Set the root visual to allow the application to render
  106. if (RootVisual != RootFrame)
  107. RootVisual = RootFrame;
  108. // Remove this handler since it is no longer needed
  109. RootFrame.Navigated -= CompleteInitializePhoneApplication;
  110. }
  111. #endregion
  112. }
  113. }