从 UI 自动化提供程序中引发事件

更新:2007 年 11 月

本主题包含演示如何从 UI 自动化提供程序中引发事件的示例代码。

示例

下面的示例在自定义按钮控件的实现中引发了 UI 自动化事件。该实现使 UI 自动化客户端应用程序能够模拟按钮单击。

为了避免不必要的处理,示例将检查 ClientsAreListening 以确定是否应该引发事件。

''' <summary>
''' Responds to a button click, regardless of whether it was caused by a 
''' mouse or keyboard click or by InvokePattern.Invoke. 
''' </summary>
Private Sub OnCustomButtonClicked()

    '' TODO  Perform program actions invoked by the control.

    '' Raise an event.
    If AutomationInteropProvider.ClientsAreListening Then
        Dim args As AutomationEventArgs = _
            New AutomationEventArgs(InvokePatternIdentifiers.InvokedEvent)
        AutomationInteropProvider.RaiseAutomationEvent( _
            InvokePatternIdentifiers.InvokedEvent, Me, args)
    End If
End Sub
/// <summary>
/// Responds to a button click, regardless of whether it was caused by a mouse or
/// keyboard click or by InvokePattern.Invoke. 
/// </summary>
private void OnCustomButtonClicked()
{
    // TODO  Perform program actions invoked by the control.

    // Raise an event.
    if (AutomationInteropProvider.ClientsAreListening)
    {
        AutomationEventArgs args = new AutomationEventArgs(InvokePatternIdentifiers.InvokedEvent);
        AutomationInteropProvider.RaiseAutomationEvent(InvokePatternIdentifiers.InvokedEvent, this, args);
    }
}

请参见

概念

UI 自动化提供程序概述