Werbung

Herkunft

Kurzinformation

Adds an event handler to an object

Syntax

<# .Synopsis Adds an event handler to an object .Description Adds an event handler to an object. If the object has a resource dictionary, it will add an eventhandlers hashtable to that object and it will store the event handler, so it can be removed later. .Example $window = New-Window $window | Add-EventHandler Loaded { $this.Top = 100 } .Parameter Object The Object to add an event handler to .Parameter EventName The name of the event (i.e. Loaded) .Parameter Handler The script block that will handle the event .Parameter Extra An extra script block that will be appended to the initial script block. By default, extra adds a trap that turns terminating errors into non-terminating errors, which can result in more stable user interfaces. .Parameter PassThru If this is set, the delegate that is added to the object will be returned from the function. #> param( [Parameter(ValueFromPipeline=$true, Mandatory=$true)] [ValidateNotNull()] $Object, [Parameter(Mandatory=$true)] [String] $EventName, [ScriptBlock] $Handler, [ScriptBlock] $Extra = { trap { Write-Error $_ continue } }, [Switch] $PassThru ) process { if ($eventName.StartsWith("On_")) { $eventName = $eventName.Substring(2) } $Event = $object.GetType().GetEvent($EventName, [Reflection.BindingFlags]"IgnoreCase, Public, Instance") if (-not $Event) { Write-Error "Handler $EventName does not exist on $Object" return } $handlerType = $event.GetAddMethod().GetParameters()[0].ParameterType $realHandler = ([ScriptBlock]::Create("" +$Handler + $Extra)) -as $HandlerType if ($Object.Resources) { if (-not $Object.Resources.EventHandlers) { $Object.Resources.EventHandlers = @{} } $Object.Resources.EventHandlers."On_$EventName" = $realHandler } $object."add_$($Event.Name)".Invoke(@($realHandler)) if ($passThru) { $RealHandler } }

Beschreibung

Adds an event handler to an object. If the object has a resource dictionary, it will add an eventhandlers hashtable to that object and it will store the event handler,so it can be removed later.

Links

Liste aller Commandlets Mehr über die Windows PowerShell

Bücher zur PowerShell

Buchcover Windows PowerShell 5.0 - Das Praxishandbuch (1. Auflage 2016/5. Auflage Gesamtreihe) Windows PowerShell 5.0 - Das Praxishandbuch (1. Auflage 2016/5. Auflage Gesamtreihe)
Autoren: Dr. Holger Schwichtenberg
erschienen 2016, 996 Seiten, 49,99 €
ISBN: 3446446435
Bestellung: Amazon.de oder im Buchhandel über ISBN 3446446435

Schulungen/Seminare

Offene Seminare zur PowerShell
In-House-Seminare zur PowerShell