C# UWP应用中的暂停模式
我有一个UWP应用程序,它使用ReactiveUI for viewmodel来查看绑定。当用户登录到应用程序时(执行登录命令时),将调用以下方法:C# UWP应用中的暂停模式,c#,uwp,reactiveui,C#,Uwp,Reactiveui,我有一个UWP应用程序,它使用ReactiveUI for viewmodel来查看绑定。当用户登录到应用程序时(执行登录命令时),将调用以下方法: private void RegisterActivityTrackingFunctionality() { Container = _container; Container.Resolve<IDialogService>().IsActive = true; _dispatch
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());
}
private void registerActivityTracking功能()
{
容器=_容器;
Container.Resolve().IsActive=true;
_dispatcher=Window.Current.dispatcher;
Window.Current.corefown.PointerPressed+=corefown\u PointerPressed;
Window.Current.corefown.KeyDown+=corefown\u 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);
}
}
));
}
}
private void CheckActivityTime()
{
_logger.Info(“在CheckActivityTime方法中”);
if(Container.Resolve().isActivityMeeDeleased)
{
如果(_timer!=null)
{
_timer.Dispose();
}
Task.Run(async()=>wait CoreApplication.MainView.corewindown.Dispatcher.RunAsync(coredipatcherpriority.Normal,
() =>
{
尝试
{
Container.Resolve().IsActive=false;
var dialogService=Container.Resolve();
如果(dialogService.CurrentContentDialog!=null)
{
dialogService.CurrentContentDialog.Hide();
}
var shellViewModel=Container.Resolve();
如果(shellViewModel.Router.NavigationStack.Count>1)
{
shellViewModel.Router.NavigateBack.Execute(null);
}
}
捕获(例外情况除外)
{
_logger.Error(“注销机制的UI线程中发生未处理的异常”,ex);
}
}
));
}
}
问题是这样的:当应用程序进入暂停模式时,如果不活动时间超过15分钟,它并不总是注销用户。
谁能给我解释一下,有什么问题吗?
也许定时器设置有问题?或者我不应该在UI线程上执行注销操作?您可以做的是在应用程序从挂起状态恢复时,将处理程序附加到应用程序的
恢复事件上,该事件在每次应用程序从挂起状态恢复时调用,然后检查Activitytime
并从那里锁定您的应用程序
Application.Current.Resuming += new EventHandler<Object>(App_Resuming);
Application.Current.Resuming+=新事件处理程序(App\u Resuming);
问题在于计时器工作正常,但有时即使超过非活动时间也不会注销。我无法在本地重现此问题,因为在本地它工作正常。所有不当应用工作的案例都是从生产部门收到的。