مشاركة عبر


الاشتراك في أحداث Auإلىmation إلى واجهة المستخدم

ملاحظةملاحظة

Th هو الوثائق هو المقصودة لإطار عمل.NET المطورين الذين يرغبون في استخدام المدارة UI Automationالفئات المعرفة في System.Windows.Automationمساحة الاسم.للحصول على أحدث المعلومات حول UI Automation، راجع API التنفيذ التلقائي Windows: أتمتة واجه المستخدم.

يوضح هذا الموضوع كيفية الاشتراك في أحداث مرفوع عن طريق من موفري "التنفيذ التلقائي لواجهة المستخدم".

مثال

regهوters رمز المثال التالي معالج حدث للحدث الذي هو raهوed عند عنصر تحكم مثل زر هو استدعاء وإزالته عند إغلاق النموذج تطبيق. حدث هو المعرف من قبل AutomationEventالذي تم تمريره كمعلمة إلى AddAutomationEventHandler.

' 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 'SubscribeToInvoke


''' <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 'OnUIAutomationEvent

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

End Sub 'ShutdownUIA
// 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);
    }
}

يظهر المثال التالي كيفية استخدام Microsoft UI Automationللاشتراك في حدث الذي هو raهوed عند تغيير تركيز. معالج الأحداث هو unregهوtered في أسلوب الذي يمكن استدعاء على إيقاف تشغيل تطبيق، أو عند الإعلام عن أحداث واجهة المستخدم هو لم تعد مطلوبة.

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 'SubscribeToFocusChange


''' <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 'OnFocusChange

' 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 'UnsubscribeFocusChange
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);
    }
}

راجع أيضًا:

المرجع

AddAutomationEventHandler

RemoveAllEventHandlers

RemoveAutomationEventHandler

المبادئ

واجه المستخدم أتمتة نظرة عامة حول الأحداث