App.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.ApplicationModel;
  7. using Windows.ApplicationModel.Activation;
  8. using Windows.Foundation;
  9. using Windows.Foundation.Collections;
  10. using Windows.UI.ViewManagement;
  11. using Windows.UI.Xaml;
  12. using Windows.UI.Xaml.Controls;
  13. using Windows.UI.Xaml.Controls.Primitives;
  14. using Windows.UI.Xaml.Data;
  15. using Windows.UI.Xaml.Input;
  16. using Windows.UI.Xaml.Media;
  17. using Windows.UI.Xaml.Navigation;
  18. // The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=402347&clcid=0x409
  19. namespace GameStateManagementSample
  20. {
  21. /// <summary>
  22. /// Provides application-specific behavior to supplement the default Application class.
  23. /// </summary>
  24. sealed partial class App : Application
  25. {
  26. static string deviceFamily;
  27. /// <summary>
  28. /// Initializes the singleton application object. This is the first line of authored code
  29. /// executed, and as such is the logical equivalent of main() or WinMain().
  30. /// </summary>
  31. public App()
  32. {
  33. this.InitializeComponent();
  34. this.Suspending += OnSuspending;
  35. //API check to ensure the "RequiresPointerMode" property exists, ensuring project is running on build 14393 or later
  36. if (Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.Application", "RequiresPointerMode"))
  37. {
  38. //If running on the Xbox, disable the default on screen pointer
  39. if (IsXbox())
  40. {
  41. Application.Current.RequiresPointerMode = ApplicationRequiresPointerMode.WhenRequested;
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// Detection code in Windows 10 to identify the platform it is being run on
  47. /// This function returns true if the project is running on an XboxOne
  48. /// </summary>
  49. public static bool IsXbox()
  50. {
  51. if (deviceFamily == null)
  52. deviceFamily = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily;
  53. return deviceFamily == "Windows.Xbox";
  54. }
  55. /// <summary>
  56. /// Invoked when the application is launched normally by the end user. Other entry points
  57. /// will be used such as when the application is launched to open a specific file.
  58. /// </summary>
  59. /// <param name="e">Details about the launch request and process.</param>
  60. protected override void OnLaunched(LaunchActivatedEventArgs e)
  61. {
  62. // By default we want to fill the entire core window.
  63. ApplicationView.GetForCurrentView().SetDesiredBoundsMode(ApplicationViewBoundsMode.UseCoreWindow);
  64. #if DEBUG
  65. if (System.Diagnostics.Debugger.IsAttached)
  66. {
  67. this.DebugSettings.EnableFrameRateCounter = true;
  68. }
  69. #endif
  70. Frame rootFrame = Window.Current.Content as Frame;
  71. // Do not repeat app initialization when the Window already has content,
  72. // just ensure that the window is active
  73. if (rootFrame == null)
  74. {
  75. // Create a Frame to act as the navigation context and navigate to the first page
  76. rootFrame = new Frame();
  77. rootFrame.NavigationFailed += OnNavigationFailed;
  78. if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
  79. {
  80. //TODO: Load state from previously suspended application
  81. }
  82. // Place the frame in the current Window
  83. Window.Current.Content = rootFrame;
  84. }
  85. if (rootFrame.Content == null)
  86. {
  87. // When the navigation stack isn't restored navigate to the first page,
  88. // configuring the new page by passing required information as a navigation
  89. // parameter
  90. rootFrame.Navigate(typeof(GamePage), e.Arguments);
  91. }
  92. // Ensure the current window is active
  93. Window.Current.Activate();
  94. }
  95. /// <summary>
  96. /// Invoked when Navigation to a certain page fails
  97. /// </summary>
  98. /// <param name="sender">The Frame which failed navigation</param>
  99. /// <param name="e">Details about the navigation failure</param>
  100. void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
  101. {
  102. throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
  103. }
  104. /// <summary>
  105. /// Invoked when application execution is being suspended. Application state is saved
  106. /// without knowing whether the application will be terminated or resumed with the contents
  107. /// of memory still intact.
  108. /// </summary>
  109. /// <param name="sender">The source of the suspend request.</param>
  110. /// <param name="e">Details about the suspend request.</param>
  111. private void OnSuspending(object sender, SuspendingEventArgs e)
  112. {
  113. var deferral = e.SuspendingOperation.GetDeferral();
  114. //TODO: Save application state and stop any background activity
  115. deferral.Complete();
  116. }
  117. }
  118. }