如何防止屏幕在 UWP 10 上锁定

How to prevent the screen from locking on UWP 10

如果用户一段时间未与 phone 交互,我想防止 phone 锁定。 在 win8 phone 开发中我使用了 PhoneApplicationService.UserIdleDetectionMode 属性。不幸的是,我找不到任何类似的 win 10 通用应用程序。 有什么建议吗?

你想要 DisplayRequest class 中的 Windows 10.

简单回答

DisplayRequest class

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

详细解答

使用显示请求来保持显示会消耗大量电量。使用显示请求时,请使用这些指南以获得最佳应用行为。

  1. 仅在需要时使用显示请求,即在不需要用户输入但显示应保持打开的时候。例如,在全屏演示期间或用户正在阅读电子书时。

  2. 不再需要时立即释放每个显示请求。

  3. 应用挂起时释放所有显示请求。如果仍然要求显示保持开启,应用程序可以在重新激活时创建新的显示请求。

请求保持显示

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; 
    } 
}

解除保持显示请求

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; 
    }
} 

参考文献 - Prevent the screen from locking on Universal Windows Platform

希望对大家有所帮助!!