App.xaml.cpp 4.0 KB

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