App.xaml.cs 7.5 KB

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