private async Task InternalStartAsync(StartArgs startArgs)
        {
            await _startSemaphore.WaitAsync();

            if (_logStartingEvents)
            {
                _logger.Log($"{nameof(ApplicationTemplate)}.{nameof(InternalStartAsync)}({startArgs})", Category.Info, Priority.None);
            }

            // sometimes activation is rased through the base.onlaunch. We'll fix that.
            if (Interlocked.Increment(ref _started) > 1 && startArgs.StartKind == StartKinds.Launch)
            {
                startArgs.StartKind = StartKinds.Activate;
            }

            SetupExtendedSplashScreen();

            try
            {
                CallOnInitializedOnlyOnce();

                if (SuspensionUtilities.IsResuming(startArgs, out var resumeArgs))
                {
                    startArgs.StartKind = StartKinds.ResumeFromTerminate;
                    startArgs.Arguments = resumeArgs;
                }
                SuspensionUtilities.ClearSuspendDate();

                _logger.Log($"[App.OnStart(startKind:{startArgs.StartKind}, startCause:{startArgs.StartCause})]", Category.Info, Priority.None);
                OnStart(startArgs);

                _logger.Log($"[App.OnStartAsync(startKind:{startArgs.StartKind}, startCause:{startArgs.StartCause})]", Category.Info, Priority.None);
                await OnStartAsync(startArgs);
            }
            finally
            {
                _startSemaphore.Release();
            }

            void SetupExtendedSplashScreen()
            {
                if (startArgs.StartKind == StartKinds.Launch &&
                    startArgs.Arguments is IActivatedEventArgs act &&
                    Window.Current.Content is null &&
                    !(ExtendedSplashScreenFactory is null))
                {
                    try
                    {
                        Window.Current.Content = ExtendedSplashScreenFactory(act.SplashScreen);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Error during {nameof(ExtendedSplashScreenFactory)}.", ex);
                    }
                }
            }
        }
        private async Task InternalStartAsync(StartArgs startArgs)
        {
            await _startSemaphore.WaitAsync();

            if (_logStartingEvents)
            {
                _logger.Log($"{nameof(ApplicationTemplate)}.{nameof(InternalStartAsync)}({startArgs})", Category.Info, Priority.None);
            }

            // sometimes activation is rased through the base.onlaunch. We'll fix that.
            if (Interlocked.Increment(ref _started) > 1 && startArgs.StartKind == StartKinds.Launch)
            {
                startArgs.StartKind = StartKinds.Activate;
            }

            try
            {
                CallOnInitializedOnlyOnce();

                if (SuspensionUtilities.IsResuming(startArgs, out var resumeArgs))
                {
                    startArgs.StartKind = StartKinds.ResumeFromTerminate;
                    startArgs.Arguments = resumeArgs;
                }
                SuspensionUtilities.ClearSuspendDate();

                _logger.Log($"[App.OnStart(startKind:{startArgs.StartKind}, startCause:{startArgs.StartCause})]", Category.Info, Priority.None);
                OnStart(startArgs);

                _logger.Log($"[App.OnStartAsync(startKind:{startArgs.StartKind}, startCause:{startArgs.StartCause})]", Category.Info, Priority.None);
                await OnStartAsync(startArgs);

                // this is redundant, but a friendly add-on
                Window.Current.Activate();
            }
            finally
            {
                _startSemaphore.Release();
            }
        }
        public ApplicationTemplate()
        {
            InternalInitialize();
            _logger.Log("[App.Constructor()]", Category.Info, Priority.None);

            CoreApplication.Exiting += (s, e) =>
            {
                var stopArgs = new StopArgs(StopKind.CoreApplicationExiting)
                {
                    CoreApplicationEventArgs = e
                };
                OnStop(stopArgs);
                OnStopAsync(stopArgs).RunSynchronously();
            };

            WindowService.WindowCreatedCallBacks.Add(Guid.Empty, args =>
            {
                WindowService.WindowCreatedCallBacks.Remove(Guid.Empty);

                args.Window.Closed += (s, e) =>
                {
                    OnStop(new StopArgs(StopKind.CoreWindowClosed)
                    {
                        CoreWindowEventArgs = e
                    });
                    OnStopAsync(new StopArgs(StopKind.CoreWindowClosed)
                    {
                        CoreWindowEventArgs = e
                    }).RunSynchronously();
                };

                Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += async(s, e) =>
                {
                    var deferral = e.GetDeferral();
                    try
                    {
                        OnStop(new StopArgs(StopKind.CloseRequested)
                        {
                            CloseRequestedPreviewEventArgs = e
                        });
                        await OnStopAsync(new StopArgs(StopKind.CloseRequested)
                        {
                            CloseRequestedPreviewEventArgs = e
                        });
                    }
                    finally
                    {
                        deferral.Complete();
                    }
                };
            });

            base.Suspending += async(s, e) =>
            {
                SuspensionUtilities.SetSuspendDate(DateTime.Now);
                var deferral = e.SuspendingOperation.GetDeferral();
                try
                {
                    var stopArgs = new StopArgs(StopKind.Suspending)
                    {
                        SuspendingEventArgs = e
                    };
                    OnStop(stopArgs);
                    await OnStopAsync(stopArgs);
                }
                finally
                {
                    deferral.Complete();
                }
            };

            base.Resuming += async(s, e) =>
            {
                var resumeArgs = new ResumeArgs
                {
                    PreviousExecutionState = ApplicationExecutionState.Suspended,
                };
                var startArgs = new StartArgs(resumeArgs, StartKinds.ResumeInMemory);
                await InternalStartAsync(startArgs);
            };
        }