从 UI 自动化提供程序中引发事件
注意
本文档适用于想要使用 System.Windows.Automation 命名空间中定义的托管 UI 自动化类的 .NET Framework 开发人员。 有关 UI 自动化的最新信息,请参阅 Windows 自动化 API:UI 自动化。
本主题包含演示如何从 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 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);
}
}