App.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright © 2011-2012 Nokia Corporation. All rights reserved.
  3. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
  4. * Other product and company names mentioned herein may be trademarks
  5. * or trade names of their respective owners.
  6. * See LICENSE.TXT for license information.
  7. */
  8. using System;
  9. using System.Windows;
  10. using System.Windows.Navigation;
  11. using Microsoft.Phone.Controls;
  12. using Microsoft.Phone.Shell;
  13. using RSSReader.Model;
  14. namespace RSSReader
  15. {
  16. /// <summary>
  17. /// Application instance
  18. /// </summary>
  19. public partial class App : Application
  20. {
  21. /// <summary>
  22. /// Const used to signal when no selection has been made in for example a listbox
  23. /// </summary>
  24. public static readonly int NO_SELECTION = -1;
  25. /// <summary>
  26. /// True if upon deactivation application was tombstoned, false otherwise
  27. /// </summary>
  28. public static bool WasTombstoned { get; private set; }
  29. /// <summary>
  30. /// Provides easy access to the root frame of the Phone Application.
  31. /// </summary>
  32. /// <returns>The root frame of the Phone Application.</returns>
  33. public PhoneApplicationFrame RootFrame { get; private set; }
  34. /// <summary>
  35. /// Constructor for the Application object.
  36. /// </summary>
  37. public App()
  38. {
  39. WasTombstoned = false;
  40. // Global handler for uncaught exceptions.
  41. UnhandledException += Application_UnhandledException;
  42. // Show graphics profiling information while debugging.
  43. if (System.Diagnostics.Debugger.IsAttached)
  44. {
  45. // Display the current frame rate counters.
  46. //Application.Current.Host.Settings.EnableFrameRateCounter = true;
  47. // Show the areas of the app that are being redrawn in each frame.
  48. //Application.Current.Host.Settings.EnableRedrawRegions = true;
  49. // Enable non-production analysis visualization mode,
  50. // which shows areas of a page that are being GPU accelerated with a colored overlay.
  51. //Application.Current.Host.Settings.EnableCacheVisualization = true;
  52. }
  53. // Standard Silverlight initialization
  54. InitializeComponent();
  55. // Phone-specific initialization
  56. InitializePhoneApplication();
  57. }
  58. // Code to execute when the application is launching (eg, from Start)
  59. // This code will not execute when the application is reactivated
  60. private void Application_Launching(object sender, LaunchingEventArgs e)
  61. {
  62. RSSService.InitializeFeeds();
  63. }
  64. // Code to execute when the application is activated (brought to foreground)
  65. // This code will not execute when the application is first launched
  66. private void Application_Activated(object sender, ActivatedEventArgs e)
  67. {
  68. WasTombstoned = !e.IsApplicationInstancePreserved;
  69. // If we were tombstoned, load feeds
  70. if (WasTombstoned)
  71. {
  72. RSSService.InitializeFeeds();
  73. }
  74. }
  75. // Code to execute when the application is deactivated (sent to background)
  76. // This code will not execute when the application is closing
  77. private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  78. {
  79. RSSService.PersistCache();
  80. }
  81. // Code to execute when the application is closing (eg, user hit Back)
  82. // This code will not execute when the application is deactivated
  83. private void Application_Closing(object sender, ClosingEventArgs e)
  84. {
  85. RSSService.PersistCache();
  86. }
  87. // Code to execute if a navigation fails
  88. private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
  89. {
  90. if (System.Diagnostics.Debugger.IsAttached)
  91. {
  92. // A navigation has failed; break into the debugger
  93. System.Diagnostics.Debugger.Break();
  94. }
  95. }
  96. // Code to execute on Unhandled Exceptions
  97. private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  98. {
  99. if (System.Diagnostics.Debugger.IsAttached)
  100. {
  101. // An unhandled exception has occurred; break into the debugger
  102. App.Log(e.ExceptionObject.Message);
  103. System.Diagnostics.Debugger.Break();
  104. }
  105. }
  106. #region Phone application initialization
  107. // Avoid double-initialization
  108. private bool phoneApplicationInitialized = false;
  109. // Do not add any additional code to this method
  110. private void InitializePhoneApplication()
  111. {
  112. if (phoneApplicationInitialized)
  113. return;
  114. // Create the frame but don't set it as RootVisual yet; this allows the splash
  115. // screen to remain active until the application is ready to render.
  116. RootFrame = new PhoneApplicationFrame();
  117. RootFrame.Navigated += CompleteInitializePhoneApplication;
  118. // Handle navigation failures
  119. RootFrame.NavigationFailed += RootFrame_NavigationFailed;
  120. // Ensure we don't initialize again
  121. phoneApplicationInitialized = true;
  122. }
  123. // Do not add any additional code to this method
  124. private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
  125. {
  126. // Set the root visual to allow the application to render
  127. if (RootVisual != RootFrame)
  128. RootVisual = RootFrame;
  129. // Remove this handler since it is no longer needed
  130. RootFrame.Navigated -= CompleteInitializePhoneApplication;
  131. }
  132. #endregion
  133. public static void Log(String message)
  134. {
  135. System.Diagnostics.Debug.WriteLine("[RSSREADER] " + message);
  136. }
  137. }
  138. }