App.xaml.cs 7.4 KB

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