UIAutomation Events on Powershell

I'm trying to listen UIAutomation events using Powershell, and wrote somethings like this.

$propChangeHandler= [System.Windows.Automation.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>
function Subscribe-PropertyChange
{
param(([System.Windows.Automation.AutomationElement]$element))

[System.Windows.Automation.Automation]::AddAutomationPropertyChangedEventHandler($element,
        [System.Windows.Automation.TreeScope]::Element, 
        $propChangeHandler($OnPropertyChange),
        [System.Windows.Automation]::AutomationElement.IsEnabledProperty)
}

# <summary>
# Handler for property changes.
# </summary>
# <param name="src">The source whose properties changed.</param>
# <param name="e">Event arguments.</param>
$OnPropertyChange=
{
param([System.Object]$src, [System.Windows.Automation.AutomationPropertyChangedEventArgs]$e)
    [System.Windows.Automation.AutomationElement]$sourceElement = $src -as [System.Windows.Automation.AutomationElement]
    if ($e.Property -eq [System.Windows.Automation.AutomationElement]::IsEnabledProperty)
    {
        [System.Boolean]$enabled = $e.NewValue -as [System.Boolean]
        # TODO: Do something with the new value. 
        # The element that raised the event can be identified by its runtime ID property.
        Write-Host("{0} is Enable" -f $sourceElement.Current.Name)
    }
    else
    { 
        # TODO: Handle other property-changed events.
    }
}

function Unsubscribe-PropertyChange
{
param([System.Windows.Automation.AutomationElement]$element)
    if ($propChangeHandler -ne $null)
    {
        [System.Windows.Automation.Automation]::RemoveAutomationPropertyChangedEventHandler($element, $propChangeHandler)
    }
}

But I got a problem.

The AddautomationPropertyChangedEventHandlermethod will need a AutomationPropertyChangedEventHandler (with an eventHandler as parameter) as parameter, but both AutomationPropertyChangedEventHandler and eventHandler are functions. 

That means function(function(function())).

(Please find the $propChangeHandler($OnPropertyChange) from the above code.)

Obviously writing it as $propChangeHandler($OnPropertyChange) is not correct. How can I revise?

 

Question Info


Last updated August 18, 2020 Views 4 Applies to: