PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

Posts Tagged ‘WPK

How to deal with -RoutedEvent parameter in WPK

with 3 comments

After ScriptEditor add-ons having been brought to the World, we were asked by our community enthusiasts about how to handle events.

The problem is that self-contained environments (also known as containers, please refer to about_scopes text file) are not described well. There are in Powershell three more or less contradictory things as follows:

– scopes

– containers that don’t or almost don’t obey the rules of scoping

– three types of variables: user-created, automatic and preference

I’m planning to give an article later this week, but at the moment I’m putting out the immediate example meant to improve the situation.

The story began from a question asked by some guy who created a universal add-on, targeted to work in both powershell_ise and PowerGUI environments. The exact complaint was that ‘my window starts from a command, but in the ise IDE events fired, whereas in the PowerGUI IDE events don’t fire’.

Not planning to spent time right now discussing why MSFT’s containers work better them Quest’s ones, I’d like to grab your attention to the following code sample which is a module, but can also be used as a script:

cls
#Only to be run in the ScriptEditor
if ($host.Name –eq 'PowerGUIScriptEditorHost') {
#region function
function script:Show-TestWindow {
    param($UseGlobal = $false)
    #region event handler
    $global:evt = 
    {    #Declaration of event variables
        param(
            $Event, 
            $EventSubscriber, 
            $Sender, 
            $SourceArgs, 
            $SourceEventArgs)
        #Variant #1
        if ($Event -ne $null) {Write-Host '001' $Event;}
        if ($EventSubscriber -ne $null) {Write-Host '002' $EventSubscriber;}
        if ($Sender -ne $null) {Write-Host '003' $Sender;}
        if ($SourceArgs -ne $null) {Write-Host '004' $SourceArgs;}
        if ($SourceEventArgs -ne $null) {Write-Host '005' $SourceEventArgs;}
        if ($This -ne $null) {write-host '006' $This;}
        if ($global:Event -ne $null) {Write-Host '011 global' $global:Event;}
        if ($global:EventSubscriber -ne $null) 
        {Write-Host '012 global' $global:EventSubscriber;}
        if ($global:Sender -ne $null) 
        {Write-Host '013 global' $global:Sender;}
        if ($global:SourceArgs -ne $null) 
        {Write-Host '014 global' $global:SourceArgs;}
        if ($global:SourceEventArgs -ne $null) 
        {Write-Host '015 global' $global:SourceEventArgs;}
        if ($global:This -ne $null) 
        {Write-Host '016 global' $global:This;}            
        #An additional demonstrable result
        [System.Windows.Forms.MessageBox]::Show("Event fired!");
    }
    #endregion event handler                                        
    #region RoutedEvent hashtable
    [System.Collections.Hashtable]$global:ht = `
        New-Object System.Collections.Hashtable;
    $global:ht.Add("Click", $global:evt);
    #endregion RoutedEvent hashtable    

    #region window
    New-Window -WindowStartupLocation CenterScreen `
        -Width 100 -Height 100 `
        -Show -On_Loaded {
        $global:btnClickMe = $window | Get-ChildControl btnClickMe
        } {
            #region layout
            New-Grid {
                #region button
                New-Button -Name btnClickMe -Margin "20,20,0,0" `
                    -Height 23 -Width 50 `
                    -HorizontalAlignment "Left" `
                    -VerticalAlignment "Top" `
                    -Content "Click Me" `
                    -On_Click $global:evt -RoutedEvent $global:ht
                #endregion button            
            }
            #endregion layout
    } 
    #endregion window
} 
#endregion function

    $se = [Quest.PowerGUI.SDK.ScriptEditorFactory]::CurrentInstance
    $cmd1 = New-Object Quest.PowerGUI.SDK.ItemCommand("Hidden", "EventTestLocal")
    #Using a scriptblock
    $cmd1.ScriptBlock = {script:Show-TestWindow $false;};
    #Or using Invoking or Invoked
    #[System.EventHandler]$global:wh = {script:Show-TestWindow; };
    #$cmd1.add_Invoked($global:wh);
    $keys = [System.Windows.Forms.Keys]::Control -bor `
        [System.Windows.Forms.Keys]::D5
    $cmd1.AddShortcut($keys)
    try{$se.Commands.Remove($cmd1);}catch{}
    $se.Commands.Add($cmd1)

Write-Host "Module WPK.Event.Test loaded";

#We can use this as a script
#Show-TestWindow
}

After turning on the module, you obtain our window by pressing Ctrl+5. Event fires at the moment you click a button providing both command line and messageboxed results.

Please notice the scoping of results printed out into the Console. $This is always global. First, all automatic variables are global. Second, there is a comment in the ‘Writing User Interfaces with WPK’ document shipped with the PowerShellPack. The doc mentioned says that $this, taken in events, ‘is where the event is coming from’.

Why $this is global instead of being script-scoped? Because containers behave slightly differently.

Additional note that I should mention is that you might declare parameters of an event as local (i.e. param($Sender)) or as global ones (that is param($global:Sender)) depending on life cycle of objects you planned.

Written by Alexander Petrovskiy

June 21, 2010 at 9:20 am