App.cpp (4522B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 5 #include "App.h" 6 #include "MainPage.h" 7 8 using namespace winrt; 9 using namespace Windows::ApplicationModel; 10 using namespace Windows::ApplicationModel::Activation; 11 using namespace Windows::Foundation; 12 using namespace Windows::UI::Xaml; 13 using namespace Windows::UI::Xaml::Controls; 14 using namespace Windows::UI::Xaml::Navigation; 15 using namespace WinGetUWPCaller; 16 using namespace WinGetUWPCaller::implementation; 17 18 /// <summary> 19 /// Initializes the singleton application object. This is the first line of authored code 20 /// executed, and as such is the logical equivalent of main() or WinMain(). 21 /// </summary> 22 App::App() 23 { 24 InitializeComponent(); 25 Suspending({ this, &App::OnSuspending }); 26 27 #if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION 28 UnhandledException([this](IInspectable const&, UnhandledExceptionEventArgs const& e) 29 { 30 if (IsDebuggerPresent()) 31 { 32 auto errorMessage = e.Message(); 33 __debugbreak(); 34 } 35 }); 36 #endif 37 } 38 39 /// <summary> 40 /// Invoked when the application is launched normally by the end user. Other entry points 41 /// will be used such as when the application is launched to open a specific file. 42 /// </summary> 43 /// <param name="e">Details about the launch request and process.</param> 44 void App::OnLaunched(LaunchActivatedEventArgs const& e) 45 { 46 Frame rootFrame{ nullptr }; 47 auto content = Window::Current().Content(); 48 if (content) 49 { 50 rootFrame = content.try_as<Frame>(); 51 } 52 53 // Do not repeat app initialization when the Window already has content, 54 // just ensure that the window is active 55 if (rootFrame == nullptr) 56 { 57 // Create a Frame to act as the navigation context and associate it with 58 // a SuspensionManager key 59 rootFrame = Frame(); 60 61 rootFrame.NavigationFailed({ this, &App::OnNavigationFailed }); 62 63 if (e.PreviousExecutionState() == ApplicationExecutionState::Terminated) 64 { 65 // Restore the saved session state only when appropriate, scheduling the 66 // final launch steps after the restore is complete 67 } 68 69 if (e.PrelaunchActivated() == false) 70 { 71 if (rootFrame.Content() == nullptr) 72 { 73 // When the navigation stack isn't restored navigate to the first page, 74 // configuring the new page by passing required information as a navigation 75 // parameter 76 rootFrame.Navigate(xaml_typename<WinGetUWPCaller::MainPage>(), box_value(e.Arguments())); 77 } 78 // Place the frame in the current Window 79 Window::Current().Content(rootFrame); 80 // Ensure the current window is active 81 Window::Current().Activate(); 82 } 83 } 84 else 85 { 86 if (e.PrelaunchActivated() == false) 87 { 88 if (rootFrame.Content() == nullptr) 89 { 90 // When the navigation stack isn't restored navigate to the first page, 91 // configuring the new page by passing required information as a navigation 92 // parameter 93 rootFrame.Navigate(xaml_typename<WinGetUWPCaller::MainPage>(), box_value(e.Arguments())); 94 } 95 // Ensure the current window is active 96 Window::Current().Activate(); 97 } 98 } 99 } 100 101 /// <summary> 102 /// Invoked when application execution is being suspended. Application state is saved 103 /// without knowing whether the application will be terminated or resumed with the contents 104 /// of memory still intact. 105 /// </summary> 106 /// <param name="sender">The source of the suspend request.</param> 107 /// <param name="e">Details about the suspend request.</param> 108 void App::OnSuspending([[maybe_unused]] IInspectable const& sender, [[maybe_unused]] SuspendingEventArgs const& e) 109 { 110 // Save application state and stop any background activity 111 } 112 113 /// <summary> 114 /// Invoked when Navigation to a certain page fails 115 /// </summary> 116 /// <param name="sender">The Frame which failed navigation</param> 117 /// <param name="e">Details about the navigation failure</param> 118 void App::OnNavigationFailed(IInspectable const&, NavigationFailedEventArgs const& e) 119 { 120 throw hresult_error(E_FAIL, hstring(L"Failed to load Page ") + e.SourcePageType().Name); 121 }