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 の結果が null になるため、自動サインオフ タイマーは開始されず、アプリケーションはサインオフしません。
私が何か間違ったことをしている場合は、アドバイスしてください。