我正在使用 Calibun Micro 框架编写 WPF 应用程序。它实现了一个自动注销系统,该系统将在某个预定义的不活动期后自动将用户从应用程序中注销。我使用此处找到的方法检查不活动。
我在我的应用程序中创建了需要各种用户输入的对话框(使用 windowmanager.showdialog(viewmodel)),并且我还需要在这些对话框上实现自动注销功能。我遇到的问题是我似乎无法从对话窗口中获取 Hwnd 详细信息。我目前在我的视图模型中执行以下操作:
public class BaseViewModel : Screen
{
public BaseViewModel(User currentUser, IEventAggregator eventAggregator)
{
BaseEventAggregator = eventAggregator;
CurrentUser = currentUser;
InitializeTimer();
}
private void InitializeTimer()
{
var currentView = GetView();
if (currentView as Window != null)
{
var windowSpecificOsMessageListener = HwndSource.FromHwnd(new WindowInteropHelper(currentView as Window).Handle);
if (windowSpecificOsMessageListener != null)
{
windowSpecificOsMessageListener.AddHook(new HwndSourceHook(CallBackMethod));
}
_autoTimer = new Timer
{
Interval = Constants.Seconds * 1000
};
_autoTimer.Tick += delegate(object sender, EventArgs args)
{
_autoTimer.Stop();
_autoTimer.Enabled = false;
_autoTimer = null;
BaseEventAggregator.Publish(new SignOutEventMessage());
};
_autoTimer.Enabled = true;
}
}
private IntPtr CallBackMethod(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
{
// Listening OS message to test whether it is a user activity
if ((msg >= 0x0200 && msg <= 0x020A) || (msg <= 0x0106 && msg >= 0x00A0) || msg == 0x0021)
{
ResetAutoTimer();
}
else
{
// For debugging purpose
// If this auto logoff does not work for some user activity, you can detect the integer code of that activity using the following line.
//Then All you need to do is adding this integer code to the above if condition.
System.Diagnostics.Debug.WriteLine(msg.ToString());
}
return IntPtr.Zero;
}
}
当对话框执行 InitializeTimer 方法时,GetView 的结果为空,因此自动注销计时器不会启动,应用程序也不会注销。
请告知我是否做错了什么。