2013-07-03 27 views
0

我试图记录用户活动,但是当我尝试调用它得到它抛出一个错误说为什么我不能使用实例引用访问此方法?

"Member 'NotifyIcon.Inactivity.GetIdleTime()' cannot be accessed with an instance reference; qualify it with a type name instead" 

这是我获取用户的空闲时间自定义事件系统空闲时间的方法

private void Inactivity_Inactive(object sender, EventArgs e) 
{ 
    inactivity.GetIdleTime(); 
} 

并用代码的方法以获得空闲时间

public static uint GetIdleTime() 
{ 
    LASTINPUTINFO lastInput = new LASTINPUTINFO(); 
    lastInput.cbSize = (uint)Marshal.SizeOf(lastInput); 
    GetLastInputInfo(ref lastInput); 

    return (uint)Environment.TickCount - lastInput.dwTime; 
} 

任何和所有帮助将不胜感激=]

回答

3

静态方法不需要为其类的对象实例引用来运行,因为它们不引用任何非静态字段,属性或方法。

当C#编译器检测到您在对象引用上调用static方法时,它怀疑您想调用其他方法,并发出您看到的错误。

更换

inactivity.GetIdleTime(); 

NotifyIcon.Inactivity.GetIdleTime(); 

来解决这个问题。

+0

感谢详细的解答,您的解决方案工作= d –

1

无需实例参考静态方法。

试试这个:

NotifyIcon.Inactivity.GetIdleTime(); 
0

public static uint GetIdleTime()不是一个实例方法。

相反,你需要调用:

NotifyIcon.Inactivity.GetIdleTime(); 
相关问题