訂閱 UI 自動化事件

注意

本文件適用對象為 .NET Framework 開發人員,其想要使用 System.Windows.Automation 命名空間中定義的受控 UI 自動化類別。 如需 UI 自動化的最新資訊,請參閱 Windows 自動化 API:UI 自動化

本主題說明如何訂閱使用者介面自動化提供者所引發的事件。

範例 1

下列範例程式碼會註冊如按鈕等控制項叫用時所引發之事件的事件處理常式,並在應用程式表單關閉時將它移除。 此事件由 AutomationEvent 當做參數傳遞給 AddAutomationEventHandler

// Member variables.
AutomationElement ElementSubscribeButton;
AutomationEventHandler UIAeventHandler;

/// <summary>
/// Register an event handler for InvokedEvent on the specified element.
/// </summary>
/// <param name="elementButton">The automation element.</param>
public void SubscribeToInvoke(AutomationElement elementButton)
{
    if (elementButton != null)
    {
        Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent,
             elementButton, TreeScope.Element,
             UIAeventHandler = new AutomationEventHandler(OnUIAutomationEvent));
        ElementSubscribeButton = elementButton;
    }
}

/// <summary>
/// AutomationEventHandler delegate.
/// </summary>
/// <param name="src">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
private void OnUIAutomationEvent(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 == InvokePattern.InvokedEvent)
    {
        // TODO Add handling code.
    }
    else
    {
        // TODO Handle any other events that have been subscribed to.
    }
}

private void ShutdownUIA()
{
    if (UIAeventHandler != null)
    {
        Automation.RemoveAutomationEventHandler(InvokePattern.InvokedEvent,
            ElementSubscribeButton, UIAeventHandler);
    }
}
' Member variables.
Private ElementSubscribeButton As AutomationElement
Private UIAeventHandler As AutomationEventHandler


''' <summary>
''' Register an event handler for InvokedEvent on the specified element.
''' </summary>
''' <param name="elementButton">The automation element.</param>
Public Sub SubscribeToInvoke(ByVal elementButton As AutomationElement)
    If (elementButton IsNot Nothing) Then
        UIAeventHandler = New AutomationEventHandler(AddressOf OnUIAutomationEvent)
        Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, elementButton, _
        TreeScope.Element, UIAeventHandler)
        ElementSubscribeButton = elementButton
    End If

End Sub


''' <summary>
''' AutomationEventHandler delegate.
''' </summary>
''' <param name="src">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
Private Sub OnUIAutomationEvent(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 ex As ElementNotAvailableException
        Exit Sub
    End Try
    If e.EventId Is InvokePattern.InvokedEvent Then
        ' TODO Add handling code.
    Else
    End If
    ' TODO Handle any other events that have been subscribed to.
    Console.WriteLine("Event: " & e.EventId.ProgrammaticName)
End Sub

Private Sub ShutdownUIA()
    If (UIAeventHandler IsNot Nothing) Then
        Automation.RemoveAutomationEventHandler(InvokePattern.InvokedEvent, ElementSubscribeButton, UIAeventHandler)
    End If

End Sub

範例 2

下列範例說明如何使用 Microsoft UI 自動化,來訂閱當焦點變更時所引發的事件。 事件處理常式會在應用程式關閉或已不再需要使用者介面事件通知時呼叫的方法中移除註冊。

AutomationFocusChangedEventHandler focusHandler = null;

/// <summary>
/// Create an event handler and register it.
/// </summary>
public void SubscribeToFocusChange()
{
    focusHandler = new AutomationFocusChangedEventHandler(OnFocusChange);
    Automation.AddAutomationFocusChangedEventHandler(focusHandler);
}

/// <summary>
/// Handle the event.
/// </summary>
/// <param name="src">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
private void OnFocusChange(object src, AutomationFocusChangedEventArgs e)
{
    // TODO Add event handling code.
    // The arguments tell you which elements have lost and received focus.
}

/// <summary>
/// Cancel subscription to the event.
/// </summary>
public void UnsubscribeFocusChange()
{
    if (focusHandler != null)
    {
        Automation.RemoveAutomationFocusChangedEventHandler(focusHandler);
    }
}
Private focusHandler As AutomationFocusChangedEventHandler = Nothing


''' <summary>
''' Create an event handler and register it.
''' </summary>
Public Sub SubscribeToFocusChange()
    focusHandler = New AutomationFocusChangedEventHandler(AddressOf OnFocusChange)
    Automation.AddAutomationFocusChangedEventHandler(focusHandler)

End Sub


''' <summary>
''' Handle the event.
''' </summary>
''' <param name="src">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
Private Sub OnFocusChange(ByVal src As Object, ByVal e As AutomationFocusChangedEventArgs)

End Sub

' TODO Add event handling code.
' The arguments tell you which elements have lost and received focus.

''' <summary>
''' Cancel subscription to the event.
''' </summary>
Public Sub UnsubscribeFocusChange()
    If (focusHandler IsNot Nothing) Then
        Automation.RemoveAutomationFocusChangedEventHandler(focusHandler)
    End If

End Sub

另請參閱