PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/DataBoundApp1/App.xaml.cs

https://bitbucket.org/JeffF/wp7codemashsmackdown2012
C# | 181 lines | 105 code | 28 blank | 48 comment | 10 complexity | 97924a629d929d9394dcad2a204ea906 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.IsolatedStorage;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Animation;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using DataBoundApp1.ViewModels;
  15. using Microsoft.Phone.Controls;
  16. using Microsoft.Phone.Shell;
  17. namespace DataBoundApp1
  18. {
  19. public partial class App : Application
  20. {
  21. private static MainViewModel viewModel = null;
  22. /// <summary>
  23. /// A static ViewModel used by the views to bind against.
  24. /// </summary>
  25. /// <returns>The MainViewModel object.</returns>
  26. public static MainViewModel ViewModel
  27. {
  28. get
  29. {
  30. // Delay creation of the view model until necessary
  31. if (viewModel == null)
  32. viewModel = new MainViewModel();
  33. return viewModel;
  34. }
  35. }
  36. /// <summary>
  37. /// Provides easy access to the root frame of the Phone Application.
  38. /// </summary>
  39. /// <returns>The root frame of the Phone Application.</returns>
  40. public PhoneApplicationFrame RootFrame { get; private set; }
  41. /// <summary>
  42. /// Constructor for the Application object.
  43. /// </summary>
  44. public App()
  45. {
  46. // Global handler for uncaught exceptions.
  47. UnhandledException += Application_UnhandledException;
  48. // Standard Silverlight initialization
  49. InitializeComponent();
  50. // Phone-specific initialization
  51. InitializePhoneApplication();
  52. // Show graphics profiling information while debugging.
  53. if (System.Diagnostics.Debugger.IsAttached)
  54. {
  55. // Display the current frame rate counters.
  56. Application.Current.Host.Settings.EnableFrameRateCounter = true;
  57. // Show the areas of the app that are being redrawn in each frame.
  58. //Application.Current.Host.Settings.EnableRedrawRegions = true;
  59. // Enable non-production analysis visualization mode,
  60. // which shows areas of a page that are handed off GPU with a colored overlay.
  61. //Application.Current.Host.Settings.EnableCacheVisualization = true;
  62. // Disable the application idle detection by setting the UserIdleDetectionMode property of the
  63. // application's PhoneApplicationService object to Disabled.
  64. // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
  65. // and consume battery power when the user is not using the phone.
  66. PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
  67. }
  68. }
  69. private void SaveFavorites()
  70. {
  71. if (IsolatedStorageSettings.ApplicationSettings.Contains("fav"))
  72. {
  73. IsolatedStorageSettings.ApplicationSettings["fav"] = ViewModel.Favorites;
  74. }
  75. else
  76. {
  77. IsolatedStorageSettings.ApplicationSettings.Add("fav", ViewModel.Favorites);
  78. }
  79. }
  80. // Code to execute when the application is launching (eg, from Start)
  81. // This code will not execute when the application is reactivated
  82. private void Application_Launching(object sender, LaunchingEventArgs e)
  83. {
  84. }
  85. // Code to execute when the application is activated (brought to foreground)
  86. // This code will not execute when the application is first launched
  87. private void Application_Activated(object sender, ActivatedEventArgs e)
  88. {
  89. // Ensure that application state is restored appropriately
  90. if (!App.ViewModel.IsDataLoaded)
  91. {
  92. App.ViewModel.LoadData();
  93. }
  94. }
  95. // Code to execute when the application is deactivated (sent to background)
  96. // This code will not execute when the application is closing
  97. private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  98. {
  99. SaveFavorites();
  100. }
  101. // Code to execute when the application is closing (eg, user hit Back)
  102. // This code will not execute when the application is deactivated
  103. private void Application_Closing(object sender, ClosingEventArgs e)
  104. {
  105. SaveFavorites();
  106. }
  107. // Code to execute if a navigation fails
  108. private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
  109. {
  110. if (System.Diagnostics.Debugger.IsAttached)
  111. {
  112. // A navigation has failed; break into the debugger
  113. System.Diagnostics.Debugger.Break();
  114. }
  115. }
  116. // Code to execute on Unhandled Exceptions
  117. private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  118. {
  119. if (System.Diagnostics.Debugger.IsAttached)
  120. {
  121. // An unhandled exception has occurred; break into the debugger
  122. System.Diagnostics.Debugger.Break();
  123. }
  124. }
  125. #region Phone application initialization
  126. // Avoid double-initialization
  127. private bool phoneApplicationInitialized = false;
  128. // Do not add any additional code to this method
  129. private void InitializePhoneApplication()
  130. {
  131. if (phoneApplicationInitialized)
  132. return;
  133. // Create the frame but don't set it as RootVisual yet; this allows the splash
  134. // screen to remain active until the application is ready to render.
  135. RootFrame = new PhoneApplicationFrame();
  136. RootFrame.Navigated += CompleteInitializePhoneApplication;
  137. // Handle navigation failures
  138. RootFrame.NavigationFailed += RootFrame_NavigationFailed;
  139. // Ensure we don't initialize again
  140. phoneApplicationInitialized = true;
  141. }
  142. // Do not add any additional code to this method
  143. private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
  144. {
  145. // Set the root visual to allow the application to render
  146. if (RootVisual != RootFrame)
  147. RootVisual = RootFrame;
  148. // Remove this handler since it is no longer needed
  149. RootFrame.Navigated -= CompleteInitializePhoneApplication;
  150. }
  151. #endregion
  152. }
  153. }