我正在开发 WinRT 应用程序。其中一项要求是该应用程序应具有“定时注销”功能。 这意味着在任何屏幕上,如果应用已闲置 10 分钟,应用应注销并导航回主屏幕。
这样做的蛮力方法显然是在每个页面的每个网格上连接指针按下事件,并在触发这些事件中的任何一个时重置计时器,但我想知道是否有更优雅、更可靠的方法这样做。
谢谢, 拉杰夫
最佳答案
通过使用 DispatcherTimer
和几个事件,您可以实现这一目标。
DispatcherTimer Timer;
private void InitializeTimer()
{
Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
Window.Current.CoreWindow.PointerMoved += CoreWindow_PointerMoved;
Window.Current.CoreWindow.PointerPressed += CoreWindow_PointerPressed;
Timer = new DispatcherTimer();
Timer.Interval = TimeSpan.FromMinutes(10);
Timer.Tick += Timer_Tick;
Timer.Start();
}
private void CoreWindow_PointerPressed(CoreWindow sender, PointerEventArgs args)
{
Timer.Start();
}
private void CoreWindow_PointerMoved(CoreWindow sender, PointerEventArgs args)
{
Timer.Start();
}
private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
Timer.Start();
}
private void Timer_Tick(object sender, object e)
{
Timer.Stop();
//TODO: Do logout.
}
关于c# - WinRT 定时注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18874516/