Teoman shipahi
Teoman shipahi

Reputation: 23052

KeepScreenOnRequest.RequestActive Not Working

For my Windows Store App, I want my application to be active all the time.

I am using code below. My Device set to be go into screen lock in 10 seconds, while I am using my application it still goes into lock screen. Am I using this code incorrectly?

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    // Prevent tablet from sleeping while app is running
    Windows.System.Display.DisplayRequest KeepScreenOnRequest = new Windows.System.Display.DisplayRequest();
    KeepScreenOnRequest.RequestActive();
}

Upvotes: 2

Views: 819

Answers (2)

Romasz
Romasz

Reputation: 29792

I think the problem may be elsewhere - your DisplayRequest may be garbage collected. Try like this:

Windows.System.Display.DisplayRequest KeepScreenOnRequest;

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    KeepScreenOnRequest = new Windows.System.Display.DisplayRequest();
    // Prevent tablet from sleeping while app is running
    KeepScreenOnRequest.RequestActive();
}

Few notes:

  • of course the above code should work for the whole app, when not needed - release the request
  • putting this in OnNavigatedFrom/OnNavigatedTo may not be a good idea, unless handeled properly - for example when app is suspended (common case) after you return OnNavigated won't be called - your DisplayRequest probably won't be activated
  • you don't need to worry about releasing you request while the app goes to background, as mentioned at MSDN:

    Note Windows automatically deactivates your app's active display requests when it is moved off screen, and re-activates them when your app comes back to the foreground.

Upvotes: 3

Akshay Soam
Akshay Soam

Reputation: 1620

I think you should try it on page navigation events instead of application level events...

using Windows.System.Display;

private DisplayRequest KeepScreenOnRequest;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    if(KeepScreenOnRequest == null)
        KeepScreenOnRequest = new DisplayRequest();

    KeepScreenOnRequest.RequestActive();
}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);

    KeepScreenOnRequest.RequestRelease();
}

Again in this scenario you have to handle the request and release part on all of your app's pages individually...

Upvotes: 5

Related Questions