Detect when screen is locked UWP

痞子三分冷 提交于 2021-02-20 05:18:33

问题


How can I detect if the screen is locked in UWP apps?

When screen is locked, suspension of app occurs and I need to put some different code in case of locking screen.


回答1:


You will find your answer here: https://developerinsider.co/prevent-the-screen-from-locking-on-uwp/

For those that are not looking for a link to another post that could, or could not, potentially be helpful here is the summary

The code you would use is

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

Things to keep in mind are

  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.



回答2:


No, you can't exactly detect when a user activates the lockscreen, but you can detect changes in the lifecycle. This way you can detect when a user suspends your app.

For information about the lifecycle: https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle



来源:https://stackoverflow.com/questions/36938363/detect-when-screen-is-locked-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!