如何防止UWP 10屏幕锁定

4

我想防止手机在用户不与手机进行交互一段时间后自动锁定。 在win8手机开发中,我使用了PhoneApplicationService.UserIdleDetectionMode属性。不幸的是,对于win10通用应用程序,我找不到类似的东西。 有什么建议吗?

2个回答

13

简单回答

DisplayRequest类

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

参考资料 - 防止通用Windows平台锁屏

希望它能帮助到某些人!


2
请注意,您需要保留对DisplayRequest对象的引用(就像在较长的代码片段中一样),否则如果您使用局部变量(就像在带有“var displayRequest”的代码片段中一样),它将没有任何效果。 - giggsy

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接