App.xaml.cs 6.6 KB

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