AutomationPropertyChangedEventHandler 委托

定义

表示由 UI 自动化客户端应用程序实现的方法,该方法处理在属性更改后由 UI 自动化提供程序引发的事件。Represents the method implemented by the UI Automation client application to handle the event raised by a UI Automation provider when a property has changed.

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

引发事件的对象。The object that raised the event.

e
AutomationPropertyChangedEventArgs

有关事件的信息。Information about the event.

继承
AutomationPropertyChangedEventHandler

示例

在下面的示例中, 处理已IsEnabled订阅元素的属性中的更改。In the following example, changes in the IsEnabled property of subscribed elements are handled.


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

注解

AutomationElement CacheRequest表示的可能没有任何缓存的属性或模式,具体取决于应用程序是否在处于活动状态时订阅sender了此事件。The AutomationElement represented by sender might not have any cached properties or patterns, depending on whether the application subscribed to this event while a CacheRequest was active.

根据提供程序的实现, 属性更改事件不一定表示属性值不同;它可能只是表示已将属性设置为相同的值。Depending on the provider implementation, a property-changed event does not necessarily signify that the property value is different; it might just mean that the property was set, even to the same value. 如果你的应用程序只需要响应实际更改, 则你负责跟踪属性的状态。If your application needs to respond only to actual changes, it is your responsibility to track the state of the property.

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅