UWP: Detect app gaining/losing focus

前端 未结 4 993
忘了有多久
忘了有多久 2020-12-16 20:20

I want to be able to prevent the screen saver from triggering while my app is in use by using the DisplayRequest class, but I only want to do this while it\'s the active app

4条回答
  •  暖寄归人
    2020-12-16 20:53

    I use the visibility changed event on the home window. The event is not fired when opening or closing a new window within the app.

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

提交回复
热议问题