Jor*_*dan 5 wpf mvvm event-hooking
我有一个MVVM自助服务终端应用程序,当它在一段时间内处于非活动状态时我需要重新启动.我正在使用Prism和Unity来促进MVVM模式.我重新启动了,我甚至知道如何处理计时器.我想知道的是如何知道什么时候发生了活动,即任何鼠标事件.我知道如何做到这一点的唯一方法是订阅主窗口的预览鼠标事件.这打破了MVVM的想法,不是吗?
我已经考虑将我的窗口暴露为将这些事件暴露给我的应用程序的接口,但这需要窗口实现该接口,这似乎也打破了MVVM.
另一种选择是使用 Windows API 方法GetLastInputInfo。
一些注意事项
使用方法很简单。调用 UserIdleMonitor.RegisterForNotification。您传入通知方法和 TimeSpan。如果用户活动发生,然后在指定的时间内停止,则调用通知方法。您必须重新注册才能收到另一条通知,并且可以随时取消注册。如果 49.7 天(加上idlePeriod)没有任何活动,则会调用通知方法。
public static class UserIdleMonitor
{
static UserIdleMonitor()
{
registrations = new List<Registration>();
timer = new DispatcherTimer(TimeSpan.FromSeconds(1.0), DispatcherPriority.Normal, TimerCallback, Dispatcher.CurrentDispatcher);
}
public static TimeSpan IdleCheckInterval
{
get { return timer.Interval; }
set
{
if (Dispatcher.CurrentDispatcher != timer.Dispatcher)
throw new InvalidOperationException("UserIdleMonitor can only be used from one thread.");
timer.Interval = value;
}
}
public sealed class Registration
{
public Action NotifyMethod { get; private set; }
public TimeSpan IdlePeriod { get; private set; }
internal uint RegisteredTime { get; private set; }
internal Registration(Action notifyMethod, TimeSpan idlePeriod)
{
NotifyMethod = notifyMethod;
IdlePeriod = idlePeriod;
RegisteredTime = (uint)Environment.TickCount;
}
}
public static Registration RegisterForNotification(Action notifyMethod, TimeSpan idlePeriod)
{
if (notifyMethod == null)
throw new ArgumentNullException("notifyMethod");
if (Dispatcher.CurrentDispatcher != timer.Dispatcher)
throw new InvalidOperationException("UserIdleMonitor can only be used from one thread.");
Registration registration = new Registration(notifyMethod, idlePeriod);
registrations.Add(registration);
if (registrations.Count == 1)
timer.Start();
return registration;
}
public static void Unregister(Registration registration)
{
if (registration == null)
throw new ArgumentNullException("registration");
if (Dispatcher.CurrentDispatcher != timer.Dispatcher)
throw new InvalidOperationException("UserIdleMonitor can only be used from one thread.");
int index = registrations.IndexOf(registration);
if (index >= 0)
{
registrations.RemoveAt(index);
if (registrations.Count == 0)
timer.Stop();
}
}
private static void TimerCallback(object sender, EventArgs e)
{
LASTINPUTINFO lii = new LASTINPUTINFO();
lii.cbSize = Marshal.SizeOf(typeof(LASTINPUTINFO));
if (GetLastInputInfo(out lii))
{
TimeSpan idleFor = TimeSpan.FromMilliseconds((long)unchecked((uint)Environment.TickCount - lii.dwTime));
//Trace.WriteLine(String.Format("Idle for {0}", idleFor));
for (int n = 0; n < registrations.Count; )
{
Registration registration = registrations[n];
TimeSpan registeredFor = TimeSpan.FromMilliseconds((long)unchecked((uint)Environment.TickCount - registration.RegisteredTime));
if (registeredFor >= idleFor && idleFor >= registration.IdlePeriod)
{
registrations.RemoveAt(n);
registration.NotifyMethod();
}
else n++;
}
if (registrations.Count == 0)
timer.Stop();
}
}
private static List<Registration> registrations;
private static DispatcherTimer timer;
private struct LASTINPUTINFO
{
public int cbSize;
public uint dwTime;
}
[DllImport("User32.dll")]
private extern static bool GetLastInputInfo(out LASTINPUTINFO plii);
}
Run Code Online (Sandbox Code Playgroud)
更新
修复了如果您尝试通过通知方法重新注册可能会陷入僵局的问题。
修复了未签名的数学并未经检查地添加了。
对计时器处理程序进行轻微优化,仅根据需要分配通知。
注释掉调试输出。
更改为使用 DispatchTimer。
添加了取消注册的功能。
在公共方法中添加了线程检查,因为这不再是线程安全的。
| 归档时间: |
|
| 查看次数: |
1699 次 |
| 最近记录: |