UWP应用程序中的暂停模式

时间:2017-11-14 10:41:37

标签: c# uwp reactiveui

我有一个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());
    }

创建计时器以监视应用程序中的用户活动。如果用户处于非活动状态超过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);
                }
            }
            ));
        }
    }

问题是这样的:当应用程序进入暂停模式时,如果不活动时间超过15分钟,它并不总是注销用户。 任何人都可以向我解释,问题是什么? 也许定时器设置有问题?或者我可能不应该在UI线程上进行注销操作?

1 个答案:

答案 0 :(得分:1)

您可以做的是将处理程序附加到应用程序的<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="image-text"> <a data-toggle="collapse" aria-expanded="false" class="collapsed"> Read More</a> <div class="collapse" aria-expanded="false" style="height: 0px;"></div> </div> <div class="image-text"> <a data-toggle="collapse" aria-expanded="false" class="collapsed"> Read More</a> <div class="collapse" aria-expanded="false" style="height: 0px;"></div> </div> <div class="image-text"> <a data-toggle="collapse" aria-expanded="false" class="collapsed"> Read More</a> <div class="collapse" aria-expanded="false" style="height: 0px;"></div> </div>事件,每次应用程序从暂停状态恢复时调用该事件,然后检查OnResuming并从那里锁定您的应用程序。

Activitytime
相关问题