App.xaml.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // App.xaml.cpp
  3. // Implementation of the App class.
  4. //
  5. #include "pch.h"
  6. #include "MainPage.xaml.h"
  7. using namespace VSXaml;
  8. using namespace Platform;
  9. using namespace Windows::ApplicationModel;
  10. using namespace Windows::ApplicationModel::Activation;
  11. using namespace Windows::Foundation;
  12. using namespace Windows::Foundation::Collections;
  13. using namespace Windows::UI::Xaml;
  14. using namespace Windows::UI::Xaml::Controls;
  15. using namespace Windows::UI::Xaml::Controls::Primitives;
  16. using namespace Windows::UI::Xaml::Data;
  17. using namespace Windows::UI::Xaml::Input;
  18. using namespace Windows::UI::Xaml::Interop;
  19. using namespace Windows::UI::Xaml::Media;
  20. using namespace Windows::UI::Xaml::Navigation;
  21. // The Blank Application template is documented at
  22. // http://go.microsoft.com/fwlink/?LinkId=234227
  23. /// <summary>
  24. /// Initializes the singleton application object. This is the first line of
  25. /// authored code executed, and as such is the logical equivalent of main()
  26. /// or WinMain().
  27. /// </summary>
  28. App::App()
  29. {
  30. InitializeComponent();
  31. Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
  32. }
  33. /// <summary>
  34. /// Invoked when the application is launched normally by the end user.
  35. /// Other entry points will be used such as when the application is
  36. /// launched to open a specific file.
  37. /// </summary>
  38. /// <param name="e">Details about the launch request and process.</param>
  39. void App::OnLaunched(
  40. Windows::ApplicationModel::Activation::LaunchActivatedEventArgs ^ e)
  41. {
  42. #if _DEBUG
  43. // Show graphics profiling information while debugging.
  44. if (IsDebuggerPresent()) {
  45. // Display the current frame rate counters
  46. DebugSettings->EnableFrameRateCounter = true;
  47. }
  48. #endif
  49. auto rootFrame = dynamic_cast<Frame ^>(Window::Current->Content);
  50. // Do not repeat app initialization when the Window already has content,
  51. // just ensure that the window is active
  52. if (rootFrame == nullptr) {
  53. // Create a Frame to act as the navigation context and associate it with
  54. // a SuspensionManager key
  55. rootFrame = ref new Frame();
  56. // Set the default language
  57. rootFrame->Language =
  58. Windows::Globalization::ApplicationLanguages::Languages->GetAt(0);
  59. rootFrame->NavigationFailed +=
  60. ref new Windows::UI::Xaml::Navigation::NavigationFailedEventHandler(
  61. this, &App::OnNavigationFailed);
  62. if (e->PreviousExecutionState == ApplicationExecutionState::Terminated) {
  63. // TODO: Restore the saved session state only when appropriate,
  64. // scheduling the
  65. // final launch steps after the restore is complete
  66. }
  67. if (rootFrame->Content == nullptr) {
  68. // When the navigation stack isn't restored navigate to the first page,
  69. // configuring the new page by passing required information as a
  70. // navigation
  71. // parameter
  72. rootFrame->Navigate(TypeName(MainPage::typeid), e->Arguments);
  73. }
  74. // Place the frame in the current Window
  75. Window::Current->Content = rootFrame;
  76. // Ensure the current window is active
  77. Window::Current->Activate();
  78. } else {
  79. if (rootFrame->Content == nullptr) {
  80. // When the navigation stack isn't restored navigate to the first page,
  81. // configuring the new page by passing required information as a
  82. // navigation
  83. // parameter
  84. rootFrame->Navigate(TypeName(MainPage::typeid), e->Arguments);
  85. }
  86. // Ensure the current window is active
  87. Window::Current->Activate();
  88. }
  89. }
  90. /// <summary>
  91. /// Invoked when application execution is being suspended. Application state
  92. /// is saved without knowing whether the application will be terminated or
  93. /// resumed with the contents of memory still intact.
  94. /// </summary>
  95. /// <param name="sender">The source of the suspend request.</param>
  96. /// <param name="e">Details about the suspend request.</param>
  97. void App::OnSuspending(Object ^ sender, SuspendingEventArgs ^ e)
  98. {
  99. (void)sender; // Unused parameter
  100. (void)e; // Unused parameter
  101. // TODO: Save application state and stop any background activity
  102. }
  103. /// <summary>
  104. /// Invoked when Navigation to a certain page fails
  105. /// </summary>
  106. /// <param name="sender">The Frame which failed navigation</param>
  107. /// <param name="e">Details about the navigation failure</param>
  108. void App::OnNavigationFailed(
  109. Platform::Object ^ sender,
  110. Windows::UI::Xaml::Navigation::NavigationFailedEventArgs ^ e)
  111. {
  112. throw ref new FailureException("Failed to load Page " +
  113. e->SourcePageType.Name);
  114. }