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