Wednesday, December 30, 2009

WPK--Tough to learn but very rewarding.

Well I've been trying to figure out how to build an app with WPK (Windodws Presentation Framework PowerShell Kit).  James Brundage has some really nice videos you can check out here.  The main site for WPK is here (which is really the home site for the PowerShellPack).  WPK is just one module that comes in the PowerShellPack.

The reason I say "Tough to learn..." is due to the general lack of good documentation.  For example if I get-help new-textbox I get all of the different parameters all jumbled up together.  If I use the -full switch on get-help it displays all of the parameters individually, but there is no explanation on how to use the parameters or what kind of data to feed the parameters (the jumbled parameters in some cases show what type of data the parameter is looking for).  So within PowerShell's help system (outside of finding parameters) it becomes difficult figuring out how to use the parameters.  For example one parameter on almost all WPK new-* cmdlets is the -DataBinding parameter, but there is nothing within PowerShell that explains how to use that parameter.  If you look online, the data you feed that parameter is very complex.  So then you go to the WPF MS site and try to interpret C# into PowerShell because none of the examples on the WPF site show how to use PowerShell cmdlets.  For me it turned into a guess fest.

So, yes, tough to learn.  However, after ton's of digging today, I found very rewarding what I can actually do with WPK once you know how it works.  It'll be nice to see some good documentation in one place in the future.  If someone is aware of a such a place please let me know.

Anyway, I started playing around with it today and had problems referencing other objects inside the main window.  For example if I wanted to change the content of a label with the text from a TextBox, I was finding it difficult to do.  I found a bunch of information on -DataBinding (which I have to say was at a minimum confusing).  Again, there is so little documentation on WPF as it relates to PowerShell cmdlets.  I spent hours trying to make the text on one TextBox equal to the text in another Textbox.  Well it ends up that the best place to find answers are the examples that get installed when you install the PowerShellPack.  I found the examples on my %systemdirve%\Users\%username%\My Documents\WindowsPowerShell\Modules\WPK\Examples.

Here is a quick script that sort of emulates a chat window.

New-Grid -Name gGrid1 -Background  blue -Rows 2 -Columns "auto","1*" -On_Loaded {            
    $script:AccData = $window | Get-ChildControl AccData            
    $script:Data = $window | Get-ChildControl Data            
    $AccData.background = "black"            
    $AccData.foreground = "lime"            
} -Children {            
    New-Label "Data" -Row 0 -Column 0 -Foreground "yellow"            
    New-TextBox -TextWrapping Wrap -AcceptsReturn -Row 0 -Column 1 -Name Data -On_PreviewKeyUp {            
        if($_.key -eq "Return"){                      
            $AccData.text = $AccData.text + $this.text            
            $this.text = ""            
    New-Label "Accumulated Data" -Row 1 -Column 0 -Foreground "yellow"            
    New-TextBox -IsReadOnly -TextWrapping Wrap -Row 1 -Column 1 -Name AccData            
} -asjob
Of course I'm running PowerShell v2.0 RTM on Windows 7.  I had to install the PowerShellPack for this to work.

Have fun.

No comments:

Post a Comment