Application.Deactivated 事件

定义

当应用程序停止作为前台应用程序时发生。

public:
 event EventHandler ^ Deactivated;
public event EventHandler Deactivated;
member this.Deactivated : EventHandler 
Public Custom Event Deactivated As EventHandler 
Public Event Deactivated As EventHandler 

事件类型

EventHandler

示例

以下示例演示如何检测独立应用程序何时停用和激活。

<Application 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.App"
  StartupUri="MainWindow.xaml"
  Activated="App_Activated" 
  Deactivated="App_Deactivated" />
using System;
using System.Windows;

namespace SDKSample
{
    public partial class App : Application
    {
        bool isApplicationActive;

        void App_Activated(object sender, EventArgs e)
        {
            // Application activated
            this.isApplicationActive = true;
        }

        void App_Deactivated(object sender, EventArgs e)
        {
            // Application deactivated
            this.isApplicationActive = false;
        }
    }
}

Imports System.Windows

Namespace SDKSample
    Partial Public Class App
        Inherits Application
        Private isApplicationActive As Boolean

        Private Sub App_Activated(ByVal sender As Object, ByVal e As EventArgs)
            ' Application activated
            Me.isApplicationActive = True
        End Sub

        Private Sub App_Deactivated(ByVal sender As Object, ByVal e As EventArgs)
            ' Application deactivated
            Me.isApplicationActive = False
        End Sub
    End Class
End Namespace

注解

具有一个或多个打开窗口的Windows Presentation Foundation应用程序在用户执行以下操作时停止成为前台应用程序) (停用:

  • 通过使用 ALT+TAB 或使用任务管理器切换到另一个应用程序。

  • 单击另一个应用程序中窗口的任务栏按钮。

需要检测停用的应用程序可以处理事件 Deactivated

首次激活应用程序后,可能会在应用程序生存期内多次停用和重新激活。 如果应用程序的行为或状态取决于其激活状态,它可以处理 DeactivatedActivated 事件以确定它所在的状态。

Deactivated) XBAP (XAML 浏览器应用程序不会引发。

适用于

另请参阅