使用UIAutomation处理ProgressBar的价值变化

时间:2014-05-31 12:48:59

标签: microsoft-ui-automation

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

1 个答案:

答案 0 :(得分:1)

我直接从MSDN documentation绘制了此示例,只更改了属性:

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);
    }
}