UWP: detect application for gain / loss of focus

I want to prevent the screen saver from starting while my application is using the DisplayRequest class, but I want to do this only in the active application. If the user switches to another window / application, I want to act as a good citizen and again allow the screensaver.

I don’t see an obvious way to detect when a UWP application receives / loses focus (or is activated / deactivated), and a quick search around does not provide any information. Can someone point me in the right direction?

+5
source share
4 answers

You should not process application activation state changes.

See How to prevent screen locks in your UWP applications .

Automatic request processing

Without any additional coding, your RequestActive () request will also be deactivated if your application no longer has focus, in other words, when your application goes into a suspended state. On Windows Phone mobile and on a PC with Windows 10 or Surface Pro in tablet mode, “your application no longer has focus” means when your application is no longer in the foreground (for example, apps with a binding are still considered to be in the foreground) However, on a PC with Windows 10 in desktop mode, this will mean when your application is minimized. Please note that even if another application window closes your application window, your application is still considered the life cycle of the UWP application, which will run in the foreground.

It's really great that when your application comes back to the forefront or is minimized, the RequestActive () request is automatically activated. You do not need to do anything!

It also means that if another application requests an active display, it cannot capture the behavior of every other application on the same device. The request is only good when the user is working with this application. As soon as the application is fired or minimized, Windows 10 will revert to its usual energy-saving rules, even if the application forgets to call RequestRelease ().

Finally, when your application is completed, all remaining displayed active requests are automatically cleared for you.

All this is done for your needs.

+2
source

This is actually pretty simple:

Window.Current.Activated += Current_Activated; private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e) { if (e.WindowActivationState == CoreWindowActivationState.Deactivated) { // do stuff } else { // do different stuff } } 
+10
source

We can use the CoreWindow.Activated event to detect when a UWP application is activated or deactivated. This method is started when the window completes activation or deactivation. And for a UWP application, we usually only have one window. Therefore, we can add some code to the Application.OnLaunched code, for example:

 Window.Current.CoreWindow.Activated += (sender, args) => { if (args.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated) { System.Diagnostics.Debug.WriteLine("Deactivated " + DateTime.Now); } else { System.Diagnostics.Debug.WriteLine("Activated " + DateTime.Now); } }; 

Alternatively, you can also use the CoreApplication.GetCurrentView method or CoreWindow.GetForCurrentThread to get CoreWindow .

+6
source

I am using the visibility change event in the home window. The event does not fire when you open or close a new window in the application.

 Window.Current.VisibilityChanged += OnVisibilityChanged; /// <summary> /// When the window visibility changes, the stuff happens /// </summary> /// <param name="sender">object sender</param> /// <param name="e">VisibilityChangedEventArgs e</param> private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs e) { if (!e.Visible) { // do stuff } else { // do other stuff } } 
+1
source

Source: https://habr.com/ru/post/1240486/


All Articles