App.xaml.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //*********************************************************
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  5. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  6. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  7. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  8. //
  9. //*********************************************************
  10. //
  11. // App.xaml.cpp
  12. // Implementation of the App.xaml class.
  13. //
  14. #include "pch.h"
  15. #include "MainPage.xaml.h"
  16. #include "AdvancedCapture.xaml.h"
  17. #include "Common\SuspensionManager.h"
  18. using namespace SDKSample;
  19. using namespace SDKSample::Common;
  20. using namespace SDKSample::MediaCapture;
  21. using namespace Concurrency;
  22. using namespace Platform;
  23. using namespace Windows::ApplicationModel;
  24. using namespace Windows::ApplicationModel::Activation;
  25. using namespace Windows::Foundation;
  26. using namespace Windows::Foundation::Collections;
  27. using namespace Windows::UI::Core;
  28. using namespace Windows::UI::Xaml;
  29. using namespace Windows::UI::Xaml::Controls;
  30. using namespace Windows::UI::Xaml::Controls::Primitives;
  31. using namespace Windows::UI::Xaml::Data;
  32. using namespace Windows::UI::Xaml::Input;
  33. using namespace Windows::UI::Xaml::Interop;
  34. using namespace Windows::UI::Xaml::Media;
  35. using namespace Windows::UI::Xaml::Navigation;
  36. /// <summary>
  37. /// Initializes the singleton application object. This is the first line of authored code
  38. /// executed, and as such is the logical equivalent of main() or WinMain().
  39. /// </summary>
  40. App::App()
  41. {
  42. InitializeComponent();
  43. this->Suspending += ref new SuspendingEventHandler(this, &SDKSample::App::OnSuspending);
  44. }
  45. /// <summary>
  46. /// Invoked when the application is launched normally by the end user. Other entry points will
  47. /// be used when the application is launched to open a specific file, to display search results,
  48. /// and so forth.
  49. /// </summary>
  50. /// <param name="pArgs">Details about the launch request and process.</param>
  51. void App::OnLaunched(LaunchActivatedEventArgs^ pArgs)
  52. {
  53. this->LaunchArgs = pArgs;
  54. // Do not repeat app initialization when already running, just ensure that
  55. // the window is active
  56. if (pArgs->PreviousExecutionState == ApplicationExecutionState::Running)
  57. {
  58. Window::Current->Activate();
  59. return;
  60. }
  61. // Create a Frame to act as the navigation context and associate it with
  62. // a SuspensionManager key
  63. auto rootFrame = ref new Frame();
  64. SuspensionManager::RegisterFrame(rootFrame, "AppFrame");
  65. auto prerequisite = task<void>([](){});
  66. if (pArgs->PreviousExecutionState == ApplicationExecutionState::Terminated)
  67. {
  68. // Restore the saved session state only when appropriate, scheduling the
  69. // final launch steps after the restore is complete
  70. prerequisite = SuspensionManager::RestoreAsync();
  71. }
  72. prerequisite.then([=]()
  73. {
  74. // When the navigation stack isn't restored navigate to the first page,
  75. // configuring the new page by passing required information as a navigation
  76. // parameter
  77. if (rootFrame->Content == nullptr)
  78. {
  79. if (!rootFrame->Navigate(TypeName(MainPage::typeid)))
  80. {
  81. throw ref new FailureException("Failed to create initial page");
  82. }
  83. }
  84. // Place the frame in the current Window and ensure that it is active
  85. Window::Current->Content = rootFrame;
  86. Window::Current->Activate();
  87. }, task_continuation_context::use_current());
  88. }
  89. /// <summary>
  90. /// Invoked when application execution is being suspended. Application state is saved
  91. /// without knowing whether the application will be terminated or resumed with the contents
  92. /// of memory still intact.
  93. /// </summary>
  94. /// <param name="sender">The source of the suspend request.</param>
  95. /// <param name="e">Details about the suspend request.</param>
  96. void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
  97. {
  98. (void) sender; // Unused parameter
  99. auto deferral = e->SuspendingOperation->GetDeferral();
  100. SuspensionManager::SaveAsync().then([=]()
  101. {
  102. deferral->Complete();
  103. });
  104. }