问题
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
- 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.
- Release each display request as soon as it is no longer required.
- 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