我一直在尝试使用 GetLastInputInfo() 监控活动。我正在使用 Windows 商店应用程序,我在 XamlTypeInfo.g.cs 中收到许多错误,说 LastInputInfo 在当前上下文中不存在,并且 MainPage 的类型在给定上下文中无效。
我是 Windows 商店应用程序的新手,我在 wpf 中对此没有任何问题。请你帮助我好吗?
这就是我使用#region LastInput int totaltime = 0;
// Declare a new instance of the structure LASTINPUTINFO contained in GetLastInputInfo
LASTINPUTINFO lastInputInf = new LASTINPUTINFO();
// The following code calls the library user32.dll GetLastInputInfo and function.
[DllImport("user32.dll")]
public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
// The DllImport attribute is very useful when reusing existing unmanaged code in a managed application.
// How can we find out within the term structure LASTINPUTINFO MarshalAs.
// Marshal is responsible to marshal data between managed and unmanaged code.
[StructLayout(LayoutKind.Sequential)]
public struct LASTINPUTINFO
{
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public int dwTime;
}
public int GetLastInputTime()
{
int idletime = 0;
lastInputInf.cbSize = Marshal.SizeOf(lastInputInf);
lastInputInf.dwTime = 0;
if (GetLastInputInfo(ref lastInputInf))
{
idletime = Environment.TickCount - lastInputInf.dwTime;
}
if (idletime != 0)
{
return idletime / 1000;
}
else
{
return 0;
}
}
public void dispatcherTimer_Tick(object sender, object e)
{
totaltime = GetLastInputTime();
if (totaltime > 10)
{
dt.Stop();
TransitionScreenSaver();
}
}