UWP 应用程序中的挂起模式

Suspended mode in the UWP application

我的 UWP 应用程序使用 ReactiveUI 视图模型来查看绑定。当用户登录到应用程序时(执行登录命令时)将调用以下方法:

private void RegisterActivityTrackingFunctionality()
    {
        Container = _container;
        Container.Resolve<IDialogService>().IsActive = true;
        _dispatcher = Window.Current.Dispatcher;

        Window.Current.CoreWindow.PointerPressed += CoreWindow_PointerPressed;
        Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
        _timer = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(30)).Subscribe(_ => CheckActivityTime());
    }

创建计时器是为了监视应用程序中的用户activity。如果用户处于非活动状态超过 15 分钟,他将从应用程序中注销。基本上为此目的调用方法 CheckActivityTime():

private void CheckActivityTime()
    {
        _logger.Info("In a CheckActivityTime Method");
        if (Container.Resolve<TokenHolder>().IsActivityTimeElapsed)
        {
            if (_timer != null)
            {
                _timer.Dispose();
            }

            Task.Run(async () => await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () =>
            {
                try
                {
                    Container.Resolve<IDialogService>().IsActive = false;

                    var dialogService = Container.Resolve<IContentDialogService>();

                    if (dialogService.CurrentContentDialog != null)
                    {
                        dialogService.CurrentContentDialog.Hide();
                    }

                    var shellViewModel = Container.Resolve<ShellViewModel>();

                    if (shellViewModel.Router.NavigationStack.Count > 1)
                    {
                        shellViewModel.Router.NavigateBack.Execute(null);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error("The unhandled exception occured in UI thread for logout mechanism", ex);
                }
            }
            ));
        }
    }

问题是这样的:当应用程序进入挂起模式时,如果 inactivity 时间超过 15 分钟,它并不总是注销用户。 谁能向我解释一下,这是什么问题? 也许定时器设置有问题?或者我不应该在 UI 线程上执行注销操作?

您可以做的是将处理程序附加到应用程序的 OnResuming 事件,每次应用程序从挂起状态恢复时调用该事件,然后检查 Activitytime 并从那里锁定您的应用程序。

Application.Current.Resuming += new EventHandler<Object>(App_Resuming);