App.xaml.cs 6.2 KB

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