Automation.AddStructureChangedEventHandler 方法

定义

注册一个将处理 structure-changed 事件的方法。

public:
 static void AddStructureChangedEventHandler(System::Windows::Automation::AutomationElement ^ element, System::Windows::Automation::TreeScope scope, System::Windows::Automation::StructureChangedEventHandler ^ eventHandler);
public static void AddStructureChangedEventHandler (System.Windows.Automation.AutomationElement element, System.Windows.Automation.TreeScope scope, System.Windows.Automation.StructureChangedEventHandler eventHandler);
static member AddStructureChangedEventHandler : System.Windows.Automation.AutomationElement * System.Windows.Automation.TreeScope * System.Windows.Automation.StructureChangedEventHandler -> unit
Public Shared Sub AddStructureChangedEventHandler (element As AutomationElement, scope As TreeScope, eventHandler As StructureChangedEventHandler)

参数

element
AutomationElement

要与事件处理程序关联的UI 自动化元素。

scope
TreeScope

要处理的事件的范围;即,它们是在元素本身上,还是在其上级和子代上。

eventHandler
StructureChangedEventHandler

发生 structure-changed 事件时调用的方法。

示例

下面的示例演示了一个结构更改的事件处理程序委托,每当指定 AutomationElement 树的子树发生更改时,就会调用该委托。

/// <summary>
/// Handles structure-changed events. If a new app window has been added, this method ensures
/// it's in the list of runtime IDs and subscribed to window-close events.
/// </summary>
/// <param name="sender">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
/// <remarks>
/// An exception can be thrown by the UI Automation core if the element disappears
/// before it can be processed -- for example, if a menu item is only briefly visible. 
/// This exception cannot be caught here because it crosses native/managed boundaries. 
/// In the debugger, you can ignore it and continue execution. The exception does not cause
/// a break when the executable is being run.
/// </remarks>
private void OnStructureChanged(object sender, StructureChangedEventArgs e)
{
    AutomationElement element = sender as AutomationElement;

    if (e.StructureChangeType == StructureChangeType.ChildAdded)
    {
        Object windowPattern;
        if (false == element.TryGetCurrentPattern(WindowPattern.Pattern, out windowPattern))
        {
            return;
        }
        int[] rid = e.GetRuntimeId();
        if (RuntimeIdListed(rid, savedRuntimeIds) < 0)
        {
            AddToWindowHandler(element);
            savedRuntimeIds.Add(rid);
        }
    }
}
''' <summary>
''' Handles structure-changed events. If a new app window has been added, this method ensures
''' it's in the list of runtime IDs and subscribed to window-close events.
''' </summary>
''' <param name="sender">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
''' <remarks>
''' An exception can be thrown by the UI Automation core if the element disappears
''' before it can be processed -- for example, if a menu item is only briefly visible. 
''' This exception cannot be caught here because it crosses native/managed boundaries. 
''' In the debugger, you can ignore it and continue execution. The exception does not cause
''' a break when the executable is being run.
''' </remarks>
Private Sub OnStructureChanged(ByVal sender As Object, ByVal e As StructureChangedEventArgs) 

    Dim element As AutomationElement = DirectCast(sender, AutomationElement)
    If e.StructureChangeType = StructureChangeType.ChildAdded Then
        Dim myWindowPattern As Object = Nothing
        If False = element.TryGetCurrentPattern(WindowPattern.Pattern, myWindowPattern) Then
            Return
        End If
        Dim rid As Integer() = e.GetRuntimeId()
        If RuntimeIdListed(rid, savedRuntimeIds) < 0 Then
            AddToWindowHandler(element)
            savedRuntimeIds.Add(rid)
        End If
    End If
 
End Sub

以下示例代码添加 委托的实例。

// elementRoot is an AutomationElement.
Automation.AddStructureChangedEventHandler(elementRoot, TreeScope.Children, 
    new StructureChangedEventHandler(OnStructureChanged));
' elementRoot is an AutomationElement.
Automation.AddStructureChangedEventHandler(elementRoot, TreeScope.Children, New StructureChangedEventHandler(AddressOf OnStructureChanged))

注解

eventHandler 可以是 方法的实例,也可以是对 Visual Basic) 中方法 (AddressOf 的引用。

适用于

另请参阅