Prevent the screen from locking on UWP

This post contains answer to the question, How to prevent the screen from locking on Windows Universal Platform.

Simple Answer

DisplayRequest class

var displayRequest = new DisplayRequest();
displayRequest.RequestActive(); //to request keep display on
displayRequest.RequestRelease(); //to release request of keep display on

Detailed Answer

Using display requests to keep the display on consumes a lot of power. Use these guidelines for best app behavior when using display requests.

  1. Use display requests only when required, that is, times when no user input is expected but the display should remain on. For example, during full screen presentations or when the user is reading an e-book.

  2. Release each display request as soon as it is no longer required.

  3. Release all display requests when the app is suspended. If the display is still required to remain on, the app can create a new display request when it is reactivated.

To Request keep display on
private void Activate_Click(object sender, RoutedEventArgs e) 
{ 
    if (g_DisplayRequest == null) 
	{ 
	    g_DisplayRequest = new DisplayRequest(); 
    }
    if (g_DisplayRequest != null) 
	{ 
        // This call activates a display-required request. If successful,  
        // the screen is guaranteed not to turn off automatically due to user inactivity. 
        g_DisplayRequest.RequestActive(); 
        drCount += 1; 
    } 
}
To release request of keep display on
private void Release_Click(object sender, RoutedEventArgs e) 
{ 
	// This call de-activates the display-required request. If successful, the screen 
    // might be turned off automatically due to a user inactivity, depending on the 
    // power policy settings of the system. The requestRelease method throws an exception  
    // if it is called before a successful requestActive call on this object. 
	if (g_DisplayRequest != null) 
	{
		g_DisplayRequest.RequestRelease(); 
		drCount -= 1; 
	}
} 

Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.