App.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using YJMPD_UWP.Model;
  2. using System;
  3. using Windows.ApplicationModel;
  4. using Windows.ApplicationModel.Activation;
  5. using Windows.Foundation;
  6. using Windows.Globalization;
  7. using Windows.Graphics.Display;
  8. using Windows.UI.Core;
  9. using Windows.UI.ViewManagement;
  10. using Windows.UI.Xaml;
  11. using Windows.UI.Xaml.Controls;
  12. using Windows.UI.Xaml.Navigation;
  13. namespace YJMPD_UWP
  14. {
  15. sealed partial class App : Application
  16. {
  17. public static Frame rootFrame;
  18. // =======================
  19. // SINGLETONS
  20. // =======================
  21. private static GeoTracker geo = new GeoTracker();
  22. public static GeoTracker Geo
  23. {
  24. get
  25. {
  26. return geo;
  27. }
  28. }
  29. private static CompassTracker cm = new CompassTracker();
  30. public static CompassTracker CompassTracker
  31. {
  32. get
  33. {
  34. return cm;
  35. }
  36. }
  37. public static CoreDispatcher Dispatcher
  38. {
  39. get
  40. {
  41. return Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
  42. }
  43. }
  44. // =========================
  45. // STATIC HELPER FUNCTIONS
  46. // =========================
  47. public static Size ScreenSize
  48. {
  49. get
  50. {
  51. var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
  52. var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
  53. Size size = new Size(bounds.Width * scaleFactor, bounds.Height * scaleFactor);
  54. return size;
  55. }
  56. }
  57. public static MainPage MainPage
  58. {
  59. get
  60. {
  61. Frame f = Window.Current.Content as Frame;
  62. MainPage mp = f.Content as MainPage;
  63. return mp;
  64. }
  65. }
  66. // ===============================
  67. // NORMAL STUFF
  68. // ===============================
  69. /// <summary>
  70. /// Initializes the singleton application object. This is the first line of authored code
  71. /// executed, and as such is the logical equivalent of main() or WinMain().
  72. /// </summary>
  73. public App()
  74. {
  75. this.InitializeComponent();
  76. this.Suspending += OnSuspending;
  77. }
  78. /// <summary>
  79. /// Invoked when the application is launched normally by the end user. Other entry points
  80. /// will be used such as when the application is launched to open a specific file.
  81. /// </summary>
  82. /// <param name="e">Details about the launch request and process.</param>
  83. protected override void OnLaunched(LaunchActivatedEventArgs e)
  84. {
  85. #if DEBUG
  86. if (System.Diagnostics.Debugger.IsAttached)
  87. {
  88. this.DebugSettings.EnableFrameRateCounter = false;
  89. }
  90. #endif
  91. rootFrame = Window.Current.Content as Frame;
  92. // Do not repeat app initialization when the Window already has content,
  93. // just ensure that the window is active
  94. if (rootFrame == null)
  95. {
  96. // Create a Frame to act as the navigation context and navigate to the first page
  97. rootFrame = new Frame();
  98. rootFrame.NavigationFailed += OnNavigationFailed;
  99. if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
  100. {
  101. //TODO: Load state from previously suspended application
  102. }
  103. // Place the frame in the current Window
  104. Window.Current.Content = rootFrame;
  105. }
  106. if (rootFrame.Content == null)
  107. {
  108. // When the navigation stack isn't restored navigate to the first page,
  109. // configuring the new page by passing required information as a navigation
  110. // parameter
  111. rootFrame.Navigate(typeof(MainPage), e.Arguments);
  112. }
  113. // Ensure the current window is active
  114. Window.Current.Activate();
  115. }
  116. /// <summary>
  117. /// Invoked when Navigation to a certain page fails
  118. /// </summary>
  119. /// <param name="sender">The Frame which failed navigation</param>
  120. /// <param name="e">Details about the navigation failure</param>
  121. void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
  122. {
  123. throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
  124. }
  125. /// <summary>
  126. /// Invoked when application execution is being suspended. Application state is saved
  127. /// without knowing whether the application will be terminated or resumed with the contents
  128. /// of memory still intact.
  129. /// </summary>
  130. /// <param name="sender">The source of the suspend request.</param>
  131. /// <param name="e">Details about the suspend request.</param>
  132. private void OnSuspending(object sender, SuspendingEventArgs e)
  133. {
  134. var deferral = e.SuspendingOperation.GetDeferral();
  135. //TODO: Save application state and stop any background activity
  136. deferral.Complete();
  137. }
  138. }
  139. }