App.xaml.cpp 4.2 KB

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