WinRT timed logout

2019-07-04 00:17发布

I am developing a WinRT app. One of the requirements is that the app should have a "timed logout" feature. What this means is that on any screen, if the app has been idle for 10 mins, the app should logout and navigate back to the home screen.

The brute force way of doing this obviously would be to hook up pointer pressed events on every grid of every page and resetting the timer if any of these events is fired but I was wondering if there was a more elegant and more reliable way of doing this.

Thanks, Rajeev

2条回答
疯言疯语
2楼-- · 2019-07-04 00:50

I'm not aware of anything built in, but rather than attaching to Grids and etc., I'd suggest you attach event handlers to the current CoreWindow (documentation) for the various types of events that you would need to track to determine idleness.

If you did attach to a Grid for example, you'd find controls that use Popup won't trigger the events. A ComboBox for example wouldn't be tracked by the event handlers.

For example, you might do this:

var core = CoreWindow.GetForCurrentThread();
core.KeyDown += (sender, kdArgs) => { 
    // reset timeout
};
core.PointerMoved = core.PointerMoved = (sender, pmArgs) {
   // reset timeout (maybe with a bit of logic to prevent tiny mouse drift from
   // causing false positives
}; 

// etc. (whatever else makes sense)

The code relies on the GetForCurrentThread call (documentation) which returns the instance of the CoreWindow that is the host for all content.

查看更多
劫难
3楼-- · 2019-07-04 01:07

With the use of DispatcherTimer & several events you can achieve that.

DispatcherTimer Timer;
private void InitializeTimer()
{
    Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
    Window.Current.CoreWindow.PointerMoved += CoreWindow_PointerMoved;
    Window.Current.CoreWindow.PointerPressed += CoreWindow_PointerPressed;

    Timer = new DispatcherTimer();
    Timer.Interval = TimeSpan.FromMinutes(10);
    Timer.Tick += Timer_Tick;
    Timer.Start();
}

private void CoreWindow_PointerPressed(CoreWindow sender, PointerEventArgs args)
{
    Timer.Start();
}

private void CoreWindow_PointerMoved(CoreWindow sender, PointerEventArgs args)
{
    Timer.Start();
}

private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
    Timer.Start();
}

private void Timer_Tick(object sender, object e)
{
    Timer.Stop();
    //TODO: Do logout.
}
查看更多
登录 后发表回答