1

当 ProgressBar 的值随 .NET UIAutomation 框架发生变化时,如何通知我?我在 AutomationElement 类中没有看到这样的属性。

4

1 回答 1

1

我直接从MSDN 文档中提取了这个示例,只更改了属性:

AutomationPropertyChangedEventHandler propChangeHandler;
/// <summary> 
/// Adds a handler for property-changed event; in particular, a change in the value 
/// </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),
        ValuePattern.ValueProperty);

}

/// <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 == ValuePattern.ValueProperty)
    {
        // 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);
    }
}
于 2014-06-04T16:27:47.313 回答