App.xaml.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // App.xaml.cpp
  3. // App 类的实现。
  4. //
  5. #include "pch.h"
  6. #include "MainPage.xaml.h"
  7. using namespace SDLPal;
  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. // “空白应用程序”模板在 http://go.microsoft.com/fwlink/?LinkID=391641 上有介绍
  23. /// <summary>
  24. /// 初始化单一实例应用程序对象。这是执行的创作代码的第一行,
  25. /// 已执行,逻辑上等同于 main() 或 WinMain()。
  26. /// </summary>
  27. App::App()
  28. {
  29. InitializeComponent();
  30. Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
  31. }
  32. /// <summary>
  33. /// 在最终用户正常启动应用程序时调用。将在启动应用程序
  34. /// 当启动应用程序以打开特定的文件或显示时使用
  35. /// 搜索结果等
  36. /// </summary>
  37. /// <param name="e">有关启动请求和过程的详细信息。</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. // 不要在窗口已包含内容时重复应用程序初始化,
  48. // 只需确保窗口处于活动状态。
  49. if (rootFrame == nullptr)
  50. {
  51. // 创建一个 Frame 以用作导航上下文并将其与
  52. // SuspensionManager 键关联
  53. rootFrame = ref new Frame();
  54. // TODO: 将此值更改为适合您的应用程序的缓存大小。
  55. rootFrame->CacheSize = 1;
  56. //设置默认语言
  57. rootFrame->Language = Windows::Globalization::ApplicationLanguages::Languages->GetAt(0);
  58. if (e->PreviousExecutionState == ApplicationExecutionState::Terminated)
  59. {
  60. // TODO: 仅当适用时还原保存的会话状态,并安排
  61. // 还原完成后的最终启动步骤。
  62. }
  63. // 将框架放在当前窗口中
  64. Window::Current->Content = rootFrame;
  65. }
  66. if (rootFrame->Content == nullptr)
  67. {
  68. // 删除用于启动的旋转门导航。
  69. if (rootFrame->ContentTransitions != nullptr)
  70. {
  71. _transitions = ref new TransitionCollection();
  72. for (auto transition : rootFrame->ContentTransitions)
  73. {
  74. _transitions->Append(transition);
  75. }
  76. }
  77. rootFrame->ContentTransitions = nullptr;
  78. _firstNavigatedToken = rootFrame->Navigated += ref new NavigatedEventHandler(this, &App::RootFrame_FirstNavigated);
  79. // 当导航堆栈尚未还原时,导航到第一页,
  80. // 并通过将所需信息作为导航参数传入来配置
  81. // 新页面。
  82. if (!rootFrame->Navigate(MainPage::typeid, e->Arguments))
  83. {
  84. throw ref new FailureException("Failed to create initial page");
  85. }
  86. }
  87. // 确保当前窗口处于活动状态
  88. Window::Current->Activate();
  89. }
  90. /// <summary>
  91. /// 启动应用程序后还原内容转换。
  92. /// </summary>
  93. void App::RootFrame_FirstNavigated(Object^ sender, NavigationEventArgs^ e)
  94. {
  95. auto rootFrame = safe_cast<Frame^>(sender);
  96. TransitionCollection^ newTransitions;
  97. if (_transitions == nullptr)
  98. {
  99. newTransitions = ref new TransitionCollection();
  100. newTransitions->Append(ref new NavigationThemeTransition());
  101. }
  102. else
  103. {
  104. newTransitions = _transitions;
  105. }
  106. rootFrame->ContentTransitions = newTransitions;
  107. rootFrame->Navigated -= _firstNavigatedToken;
  108. }
  109. void SDLPal::App::OnActivated(Windows::ApplicationModel::Activation::IActivatedEventArgs ^ args)
  110. {
  111. auto page = static_cast<SDLPal::MainPage^>(e->ContinuationData->Lookup("Page"));
  112. switch (args->Kind)
  113. {
  114. case ActivationKind::PickFolderContinuation:
  115. page->SetPath(static_cast<IFolderPickerContinuationEventArgs^>(args)->Folder);
  116. break;
  117. case ActivationKind::PickFileContinuation:
  118. {
  119. auto e = safe_cast<IFileOpenPickerContinuationEventArgs^>(args);
  120. if (e->Files->Size > 0)
  121. {
  122. auto target = static_cast<Windows::UI::Xaml::Controls::TextBox^>(e->ContinuationData->Lookup("Target"));
  123. page->SetFile(target, e->Files->GetAt(0));
  124. }
  125. break;
  126. }
  127. case ActivationKind::PickSaveFileContinuation:
  128. {
  129. auto file = safe_cast<IFileSavePickerContinuationEventArgs^>(args)->File;
  130. auto target = static_cast<Windows::UI::Xaml::Controls::TextBox^>(e->ContinuationData->Lookup("Target"));
  131. if (file) page->SetFile(target, file);
  132. break;
  133. }
  134. }
  135. Application::OnActivated(args);
  136. }
  137. /// <summary>
  138. /// 在将要挂起应用程序执行时调用。将保存应用程序状态
  139. /// 无需知道应用程序会被终止还是会恢复,
  140. /// 并让内存内容保持不变。
  141. /// </summary>
  142. void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
  143. {
  144. (void) sender; // 未使用的参数
  145. (void) e; // 未使用的参数
  146. // TODO: 保存应用程序状态并停止任何后台活动
  147. }