Automation.AddStructureChangedEventHandler Method

Definition

Registers the method that will handle structure-changed events.

public static void AddStructureChangedEventHandler(System.Windows.Automation.AutomationElement element, System.Windows.Automation.TreeScope scope, System.Windows.Automation.StructureChangedEventHandler eventHandler);

Parameters

element
AutomationElement

The UI Automation element with which to associate the event handler.

scope
TreeScope

The scope of events to be handled; that is, whether they are on the element itself, or on its ancestors and descendants.

eventHandler
StructureChangedEventHandler

The method to call when the structure-changed event occurs.

Examples

The following example shows a structure-changed event handler delegate that will be called whenever the subtree of the specified AutomationElement changes.

/// <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);
        }
    }
}

The following example code adds an instance of the delegate.

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

Remarks

eventHandler can be an instance of the method, or a reference to the method (AddressOf in Visual Basic).

Applies to

Produto Versões
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also