WindowPattern.WindowClosedEvent 字段

定义

标识在关闭窗口时引发的事件。

public: static initonly System::Windows::Automation::AutomationEvent ^ WindowClosedEvent;
public static readonly System.Windows.Automation.AutomationEvent WindowClosedEvent;
 staticval mutable WindowClosedEvent : System.Windows.Automation.AutomationEvent
Public Shared ReadOnly WindowClosedEvent As AutomationEvent 

字段值

示例

在以下示例中,声明了事件侦听器,并为 AutomationEventHandlerWindowOpenedEventWindowClosedEvent 事件指定了委托。

///--------------------------------------------------------------------
/// <summary>
/// Register for events of interest.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
///--------------------------------------------------------------------
private void RegisterForAutomationEvents(
    AutomationElement targetControl)
{
    AutomationEventHandler eventHandler = 
        new AutomationEventHandler(OnWindowOpenOrClose);
    Automation.AddAutomationEventHandler(
        WindowPattern.WindowClosedEvent, 
        targetControl, TreeScope.Element, eventHandler);
    Automation.AddAutomationEventHandler(
        WindowPattern.WindowOpenedEvent, 
        targetControl, TreeScope.Element, eventHandler);
}

///--------------------------------------------------------------------
/// <summary>
/// AutomationEventHandler delegate.
/// </summary>
/// <param name="src">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
///--------------------------------------------------------------------
private void OnWindowOpenOrClose(object src, AutomationEventArgs e)
{
    // Make sure the element still exists. Elements such as tooltips
    // can disappear before the event is processed.
    AutomationElement sourceElement;
    try
    {
        sourceElement = src as AutomationElement;
    }
    catch (ElementNotAvailableException)
    {
        return;
    }
    
    if (e.EventId == WindowPattern.WindowOpenedEvent)
    {
        // TODO: event handling
        return;
    }
    if (e.EventId == WindowPattern.WindowClosedEvent)
    {
        // TODO: event handling
        return;
    }
}
'''------------------------------------------------------------------------
''' <summary>
''' Register for events of interest.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
'''------------------------------------------------------------------------
Private Sub RegisterForEvents(ByVal targetControl As AutomationElement)
    Dim eventHandler As AutomationEventHandler = AddressOf OnWindowOpenOrClose
    Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, _
        targetControl, TreeScope.Element, eventHandler)
    Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, _
        targetControl, TreeScope.Element, eventHandler)
End Sub

'''------------------------------------------------------------------------
''' <summary>
''' AutomationEventHandler delegate.
''' </summary>
''' <param name="src">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
'''------------------------------------------------------------------------
Private Sub OnWindowOpenOrClose(ByVal src As Object, _
    ByVal e As AutomationEventArgs)
    ' Make sure the element still exists. Elements such as tooltips
    ' can disappear before the event is processed.
    Dim sourceElement As AutomationElement
    Try
        sourceElement = DirectCast(src, AutomationElement)
    Catch
        Return
    End Try

    If e.EventId Is WindowPattern.WindowOpenedEvent Then
        ' TODO: event handling
        Return
    End If
    If e.EventId Is WindowPattern.WindowClosedEvent Then
        ' TODO: event handling
        Return
    End If
End Sub

注解

客户端应用程序可能需要从缓存对象侦听 WindowClosedEvent ,因为窗口在关闭后会立即从 UI 自动化控件视图结构中删除。

此标识符由 UI 自动化客户端应用程序使用。 UI 自动化提供程序应使用 中的 WindowPatternIdentifiers等效字段。

适用于

另请参阅