App.xaml.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // App.xaml.cpp
  3. // Implementation of the App class.
  4. //
  5. #include "pch.h"
  6. #include "MainPage.xaml.h"
  7. using namespace PhoneTutorial;
  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::Media::Animation;
  21. using namespace Windows::UI::Xaml::Navigation;
  22. // The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkID=391641
  23. /// <summary>
  24. /// Initializes the singleton application object. This is the first line of authored code
  25. /// executed, and as such is the logical equivalent of main() or WinMain().
  26. /// </summary>
  27. App::App()
  28. {
  29. InitializeComponent();
  30. Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
  31. }
  32. /// <summary>
  33. /// Invoked when the application is launched normally by the end user. Other entry points
  34. /// will be used when the application is launched to open a specific file, to display
  35. /// search results, and so forth.
  36. /// </summary>
  37. /// <param name="e">Details about the launch request and process.</param>
  38. void App::OnLaunched(LaunchActivatedEventArgs^ e)
  39. {
  40. #if _DEBUG
  41. if (IsDebuggerPresent())
  42. {
  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. // TODO: Change this value to a cache size that is appropriate for your application.
  55. rootFrame->CacheSize = 1;
  56. if (e->PreviousExecutionState == ApplicationExecutionState::Terminated)
  57. {
  58. // TODO: Restore the saved session state only when appropriate, scheduling the
  59. // final launch steps after the restore is complete.
  60. }
  61. // Place the frame in the current Window
  62. Window::Current->Content = rootFrame;
  63. }
  64. if (rootFrame->Content == nullptr)
  65. {
  66. // Removes the turnstile navigation for startup.
  67. if (rootFrame->ContentTransitions != nullptr)
  68. {
  69. _transitions = ref new TransitionCollection();
  70. for (auto transition : rootFrame->ContentTransitions)
  71. {
  72. _transitions->Append(transition);
  73. }
  74. }
  75. rootFrame->ContentTransitions = nullptr;
  76. _firstNavigatedToken = rootFrame->Navigated += ref new NavigatedEventHandler(this, &App::RootFrame_FirstNavigated);
  77. // When the navigation stack isn't restored navigate to the first page,
  78. // configuring the new page by passing required information as a navigation
  79. // parameter.
  80. if (!rootFrame->Navigate(MainPage::typeid, e->Arguments))
  81. {
  82. throw ref new FailureException("Failed to create initial page");
  83. }
  84. }
  85. // Ensure the current window is active
  86. Window::Current->Activate();
  87. }
  88. /// <summary>
  89. /// Restores the content transitions after the app has launched.
  90. /// </summary>
  91. void App::RootFrame_FirstNavigated(Object^ sender, NavigationEventArgs^ e)
  92. {
  93. auto rootFrame = safe_cast<Frame^>(sender);
  94. TransitionCollection^ newTransitions;
  95. if (_transitions == nullptr)
  96. {
  97. newTransitions = ref new TransitionCollection();
  98. newTransitions->Append(ref new NavigationThemeTransition());
  99. }
  100. else
  101. {
  102. newTransitions = _transitions;
  103. }
  104. rootFrame->ContentTransitions = newTransitions;
  105. rootFrame->Navigated -= _firstNavigatedToken;
  106. }
  107. /// <summary>
  108. /// Invoked when application execution is being suspended. Application state is saved
  109. /// without knowing whether the application will be terminated or resumed with the contents
  110. /// of memory still intact.
  111. /// </summary>
  112. void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
  113. {
  114. (void) sender; // Unused parameter
  115. (void) e; // Unused parameter
  116. // TODO: Save application state and stop any background activity
  117. }