App.xaml.cs 6.1 KB

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