AutomationEventHandler 委托

定义

表示由 UI 自动化客户端应用程序实现的方法,该方法处理由 UI 自动化提供程序引发的事件。

C#
public delegate void AutomationEventHandler(object sender, AutomationEventArgs e);

参数

sender
Object

引发事件的对象。

e
AutomationEventArgs

有关事件的信息。

示例

以下示例演示如何订阅和处理事件。

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

注解

AutomationEventHandler使用委托指定客户端调用的方法来处理UI 自动化事件。

表示 AutomationElementsender 可能没有任何缓存的属性或模式,具体取决于应用程序是否在活动期间 CacheRequest 订阅了此事件。

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.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
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅