MarekT
MarekT

Reputation: 104

Best method for returning to home screen in UWP app

I am developing a UWP based kiosk app, and I would like each view to return to the home page after x amount of time has passed. What would be the best method to achieve this? I was thinking of having each page start an inactivity counter and once the counter runs down to go back home. Thoughts?

Upvotes: 0

Views: 458

Answers (1)

Vijay Nirmal
Vijay Nirmal

Reputation: 5837

I was thinking of having each page start an inactivity counter and once the counter runs down to go back home

I think that is the right way to do it.

Use DispatcherTimer for the counter.

To check inactivity, you can detect global input with various events on the app's Window.Current.CoreWindow

Touch and mouse input with Window.Current.CoreWindow.PointerPressed, PointerMoved, and PointerReleased.

Keyboard input: KeyUp and KeyDown (soft keys) and CharacterReceived (for characters generated via chords & text suggestions).

DispatcherTimer dispatcherTimer;

public NewPage()
{
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Tick += dispatcherTimer_Tick;
    dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
    dispatcherTimer.Start();
    CheckIdle();
}

public void dispatcherTimer_Tick(object sender, object e)
{
    dispatcherTimer.Tick -= dispatcherTimer_Tick;
    dispatcherTimer.Stop();
    Frame.Navigate(typeof(MainPage));
}

private void CheckIdle()
{
    //Calling DispatcherTimer.Start() will reset the timer interval
    Window.Current.CoreWindow.PointerMoved += (s, e) => dispatcherTimer.Start();
    Window.Current.CoreWindow.PointerPressed += (s, e) => dispatcherTimer.Start();
    Window.Current.CoreWindow.PointerReleased += (s, e) => dispatcherTimer.Start();
    Window.Current.CoreWindow.PointerWheelChanged += (s, e) => dispatcherTimer.Start();
    Window.Current.CoreWindow.KeyDown += (s, e) => dispatcherTimer.Start();
    Window.Current.CoreWindow.KeyUp += (s, e) => dispatcherTimer.Start();
    Window.Current.CoreWindow.CharacterReceived += (s, e) => dispatcherTimer.Start();
}

Upvotes: 1

Related Questions