如果用户闲置一段时间,我想创建一个超时并导航到主页面的函数.经过一番研究,我发现ThreadPoolTimer应该适合我的需要.测试它我决定使用10秒的间隔.
timer =ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick,TimeSpan.FromSeconds(10));
Run Code Online (Sandbox Code Playgroud)
这就是我不知所措的地方.我无法找到一种方法来检查UWP上的用户输入,而无需单独检查PointerPressed,PointerExited等.所以我做了一些挖掘,我找到了一个代码块,如果用户应该给你一个布尔值闲置与否.
public static uint GetIdleTime()
{
LASTINPUTINFO lastInPut = new LASTINPUTINFO();
lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut);
GetLastInputInfo(ref lastInPut);
return ((uint)Environment.TickCount - lastInPut.dwTime);
}
public static bool IsUserIdle()
{
uint idleTime = (uint)Environment.TickCount - GetLastInputEventTickCount();
if (idleTime > 0)
{
idleTime = (idleTime / 1000);
}
else
{
idleTime = 0;
}
//user is idle for 10 sec
bool b = (idleTime >= 10);
return b;
}
private static uint GetLastInputEventTickCount()
{
LASTINPUTINFO lii = new LASTINPUTINFO();
lii.cbSize = (uint)Marshal.SizeOf(lii); …Run Code Online (Sandbox Code Playgroud)