Reputation: 23052
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
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:
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
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