private void SetupLifecycleListeners() { Resuming += (s, e) => StartupOrchestratorAsync(OriginalActivatedArgs, StartKind.Launch); Suspending += async(s, e) => { var deferral = e.SuspendingOperation.GetDeferral(); try { if (AutoSuspendAllFrames) { await LifecycleLogic.AutoSuspendAllFramesAsync(this, e); } var isPrelaunch = (OriginalActivatedArgs as LaunchActivatedEventArgs)?.PrelaunchActivated ?? false; await OnSuspendingAsync(this, e, isPrelaunch); } finally { deferral.Complete(); } }; }
private async void StartupOrchestratorAsync(IActivatedEventArgs e, StartKind kind) { DebugWrite($"kind:{kind} previous:{e.PreviousExecutionState}"); if (OriginalActivatedArgs == null) { OriginalActivatedArgs = e; // if resume tries to launch, don't continue on // StartupOrchestratorAsync will be called twice if (OriginalActivatedArgs == null) { OnResuming(this, null, AppExecutionState.Terminated); return; } } // is the kind really right? if (kind == StartKind.Launch && CurrentStateHistory.ContainsValue(BootstrapperStates.Launched)) { kind = StartKind.Activate; } else if (kind == StartKind.Launch && e.PreviousExecutionState == ApplicationExecutionState.Running) { kind = StartKind.Activate; } else if (kind == StartKind.Activate && e.PreviousExecutionState != ApplicationExecutionState.Running) { kind = StartKind.Launch; } // handle activate if (kind == StartKind.Activate) { // never activate until launch has completed while (!CurrentStateHistory.ContainsValue(BootstrapperStates.Launched)) { await Task.Delay(10); } await OnStartAsync(kind, e); CurrentState = BootstrapperStates.Started; WindowLogic.ActivateWindow(ActivateWindowSources.Activating, SplashLogic); CurrentState = BootstrapperStates.Activated; } // handle first-time launch else if (kind == StartKind.Launch) { SplashLogic.Show(e.SplashScreen, this); // do some one-time things SetupKeyboardListeners(); SetupLifecycleListeners(); SetupSystemListeners(); SetupCustomTitleBar(); // default Unspecified extended session await ExtendedSessionService.StartAsync(); // OnInitializeAsync await OnInitializeAsync(e); CurrentState = BootstrapperStates.Initialized; // if there no pre-existing root then generate root if (SplashLogic.Splashing || Window.Current.Content == null) { Window.Current.Content = CreateRootElement(e); } // okay, now handle launch bool restored = false; var IsPrelaunch = (e as LaunchActivatedEventArgs)?.PrelaunchActivated ?? false; switch (e.PreviousExecutionState) { case ApplicationExecutionState.Suspended: case ApplicationExecutionState.Terminated: OnResuming(this, null, IsPrelaunch ? AppExecutionState.Prelaunch : AppExecutionState.Terminated); if (AutoRestoreAfterTerminated) { restored = await LifecycleLogic.AutoRestoreAsync(e as ILaunchActivatedEventArgs, NavigationService); CurrentState = BootstrapperStates.Restored; } break; } // handle if pre-launch (no UI) if (IsPrelaunch) { var runOnStartAsync = false; await OnPrelaunchAsync(e, out runOnStartAsync); CurrentState = BootstrapperStates.Prelaunched; if (!runOnStartAsync) { return; } } // handle if not restored (new launch) if (!restored) { await OnStartAsync(StartKind.Launch, e); CurrentState = BootstrapperStates.Started; } // this will also hide any showing splashscreen WindowLogic.ActivateWindow(ActivateWindowSources.Launching, SplashLogic); CurrentState = BootstrapperStates.Launched; } }