App.xaml.cs 6.4 KB

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