AutomationPropertyChangedEventHandler 委托

定义

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

public delegate void AutomationPropertyChangedEventHandler(System::Object ^ sender, AutomationPropertyChangedEventArgs ^ e);
public delegate void AutomationPropertyChangedEventHandler(object sender, AutomationPropertyChangedEventArgs e);
type AutomationPropertyChangedEventHandler = delegate of obj * AutomationPropertyChangedEventArgs -> unit
Public Delegate Sub AutomationPropertyChangedEventHandler(sender As Object, e As AutomationPropertyChangedEventArgs)

参数

sender
Object

引发事件的对象。

e
AutomationPropertyChangedEventArgs

有关事件的信息。

示例

在以下示例中,将处理已订阅元素的属性中的更改 IsEnabled


AutomationPropertyChangedEventHandler propChangeHandler;
/// <summary>
/// Adds a handler for property-changed event; in particular, a change in the enabled state.
/// </summary>
/// <param name="element">The UI Automation element whose state is being monitored.</param>
public void SubscribePropertyChange(AutomationElement element)
{
    Automation.AddAutomationPropertyChangedEventHandler(element, 
        TreeScope.Element, 
        propChangeHandler = new AutomationPropertyChangedEventHandler(OnPropertyChange),
        AutomationElement.IsEnabledProperty);
}

/// <summary>
/// Handler for property changes.
/// </summary>
/// <param name="src">The source whose properties changed.</param>
/// <param name="e">Event arguments.</param>
private void OnPropertyChange(object src, AutomationPropertyChangedEventArgs e)
{
    AutomationElement sourceElement = src as AutomationElement;
    if (e.Property == AutomationElement.IsEnabledProperty)
    {
        bool enabled = (bool)e.NewValue;
        // TODO: Do something with the new value. 
        // The element that raised the event can be identified by its runtime ID property.
    }
    else
    { 
        // TODO: Handle other property-changed events.
    }
}

public void UnsubscribePropertyChange(AutomationElement element)
{
    if (propChangeHandler != null)
    {
        Automation.RemoveAutomationPropertyChangedEventHandler(element, propChangeHandler);
    }
}

Dim propChangeHandler As AutomationPropertyChangedEventHandler

''' <summary>
''' Adds a handler for property-changed event; in particular, a change in the enabled state.
''' </summary>
''' <param name="element">The UI Automation element whose state is being monitored.</param>
Public Sub SubscribePropertyChange(ByVal element As AutomationElement)
    propChangeHandler = _
        New AutomationPropertyChangedEventHandler(AddressOf OnPropertyChange)
    Automation.AddAutomationPropertyChangedEventHandler(element, TreeScope.Element, _
        propChangeHandler, AutomationElement.IsEnabledProperty)

End Sub



''' <summary>
''' Handler for property changes.
''' </summary>
''' <param name="src">The source whose properties changed.</param>
''' <param name="e">Event arguments.</param>
Private Sub OnPropertyChange(ByVal src As Object, ByVal e As AutomationPropertyChangedEventArgs) 
    Dim sourceElement As AutomationElement = DirectCast(src, AutomationElement)
    If e.Property Is AutomationElement.IsEnabledProperty Then
        Dim enabled As Boolean = CBool(e.NewValue)
        ' TODO: Do something with the new value. 
        ' The element that raised the event can be identified by its runtime ID property.
    Else
    End If
 ' TODO: Handle other property-changed events.
End Sub


Public Sub UnsubscribePropertyChange(ByVal element As AutomationElement)
    If (propChangeHandler IsNot Nothing) Then
        Automation.RemoveAutomationPropertyChangedEventHandler(element, propChangeHandler)
    End If

End Sub

注解

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

根据提供程序实现,属性更改的事件不一定表示属性值不同;它可能只是意味着属性已设置,甚至设置为相同的值。 如果应用程序只需要响应实际更改,则需负责跟踪属性的状态。

扩展方法

GetMethodInfo(Delegate)

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

适用于

另请参阅