Handling Button Clicks in WPF with PowerShell

In my last post, I discussed how to fire up a WPF window in PowerShell. You can make XAML look pretty amazing, but if buttons and controls never get wired up to any functionality, it gets boring pretty quick. Here is the code I had last time with a small addition.Blend allows you to name your controls. This turns into what you see in line 018 in the XAML. x:Name=”Close.”

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
[xml]$xaml = @"
    <Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="Blend and PowerShell"
Width="640" Height="480" AllowsTransparency="False">
<Grid x:Name="LayoutRoot">
  <Rectangle Margin="22,8,22,0" VerticalAlignment="Top" Height="178" Stroke="#FF000000">
   <Rectangle.Fill>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
     <GradientStop Color="#FF000000" Offset="0"/>
     <GradientStop Color="#FF861A1A" Offset="1"/>
    </LinearGradientBrush>
   </Rectangle.Fill>
  </Rectangle>
  <Button Margin="121,0,129,96" VerticalAlignment="Bottom" Height="100" Content="Button"
    x:Name="Close"/>
</Grid>
</Window>
"@

$reader = New-Object System.Xml.XmlNodeReader $xaml
$d = [Windows.Markup.XamlReader]::Load($reader)
$d.FindName("Close").add_click({
    $d.Close()
})
$d.ShowDialog() | out-null

Once you have the name, you can call the FindName method (see line 024) on your WPF Window. This allows you to find the named control that you want to add event handling for. Using FindName, we can add the add_Click method and pass in the ScriptBlock that we want to execute on the click event.

When you run this, clicking the button will close the dialog box.

No Seattle Script Club this Month

I had previously mentioned that there would be another Script Club this month, but schedule wise, things did not quite work out.

My wife and I are in “getting ready for new baby” mode. The C-section is scheduled for the 1st week of June.

I am guessing the next one will be in July sometime.

Blend -> XAML -> PowerShell

The PowerShell team just posted a blog entry with slides and demo scripts for one of their presentations from TechEd 2009. There is a lot of cool stuff in their but one thing that piqued my interested was the GUI Demo using WPF. I noticed in the demo scripts that they had a PS1 script and a XAML file. I could try to describe XAML but I will leave that to the guys that literally wrote the book.

“XAML is an XML-based language for creating and initializing .NET objects. It’s used in WPF as a human-authorable way of describing the UI.” - “Programming WPF, by Chris Sells and Ian Griffiths, page 8

Expression Blend is a Microsoft product that allows designers to create UI’s in a sort of similar way that you could create WinForms using the WinForms designer in Visual Studio. It’s all drag ‘n drop. Blend is essentially a Winforms designer for WPF. It definitely takes some getting used to but the bottom line is that you don’t need to know how to code to get some basic windows, shapes, and controls up and running.

Here is a (not so) beautiful dialog box with a rectangle and a big button

image

Well, in Blend, if you click on XAML tab you can see the angle brackets that make up this UI.

What’s cool is that you can use this XAML that was created with Blend in PowerShell.

We can accomplish this using a [Windows.Markup.XamlReader] and call that Load static Method. Assuming we store the XAML in a here string, you just need to cast it to XML and pass it in like so

$reader = New-Object System.Xml.XmlNodeReader $xaml
$d = [Windows.Markup.XamlReader]::Load($reader)

Once you have $d you can do a bunch of things with it.

$d.ShowDialog() will display the dialog box as the script below demonstrates.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
[xml]$xaml = @"
    <Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="Blend and PowerShell"
Width="640" Height="480" AllowsTransparency="False">

<Grid x:Name="LayoutRoot">
  <Button HorizontalAlignment="Right"
    Margin="0,0,153,194"
    VerticalAlignment="Bottom"
    Width="5" Height="2"
    Content="Button"/>
  <Rectangle Margin="22,8,22,0"
       VerticalAlignment="Top"
       Height="178"
       Stroke="#FF000000">
   <Rectangle.Fill>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
     <GradientStop Color="#FF000000" Offset="0"/>
     <GradientStop Color="#FF861A1A" Offset="1"/>
    </LinearGradientBrush>
   </Rectangle.Fill>
  </Rectangle>
  <Button Margin="121,0,129,96"
          VerticalAlignment="Bottom"
    Height="100"
    Content="Button"
    x:Name="Close"/>
</Grid>
</Window>
"@

$reader = New-Object System.Xml.XmlNodeReader $xaml
$d = [Windows.Markup.XamlReader]::Load($reader)
$d.ShowDialog() | out-null

 

 

 

When I run this, I get the following

image

You may notice I tweaked the XAML a little bit. I changed the Window title on line 6. Blend also adds a class to its <Window> tag that is associated with the project in Blend. I had to remove that to get it to work in PowerShell. Other than that, we basically have a WPF editor that can create XAML that can be used in PowerShell.

My next post will deal with events. I do suppose it would be nice if clicking the button actually did something, huh.

Until next time..

Andy

Scott Hanselman&#8217;s Tools List

I just started rebuilding a laptop with Windows 7 RC and thought I would do a bit of a refresh on my typical tool list that gets added to my machine immediately after a clean install. Notepad2 with PowerShell support is the latest must add for me.

Scott Hanselman has a phenomenal list of tools that he posted back in 2007 but they are still worth a look through. Most are free and the ones that are not are worth paying for.

Scott is mainly a developer so a lot of the tools are dev focused, but a the top 10 would apply to anyone that wants to interact with their computer in a more effective way.

I have heard him mention on his podcast that people are starting to complain that he hasn’t refreshed the list in a while. Maybe we will have the pleasure of the 2009 list soon. We’ll see.

PowerShell and ActiveWords

I have been trying out some new software called ActiveWords. I really think this is one of those pieces of software that could potentially change the way you interact with a computer. It basically provides you with the ability to launch any app or replace any text anywhere in any context on your computer. I would recommend checking out their online video demos to see what you could do with this.

Heres a quick little video showing me using a keyword I created to add parameters to advanced functions in the ISE. I am sure you will be able to tell what is me typing versus the text replacement that ActiveWords does.

 

And here’s another video of me using the same keyword in Notepad2, which I use quite often for quick text editing.

The actions that can be taken in ActiveWords are activated using a hotkey. You can also enable the use a double space-bar which I prefer.

They have a free 60 day trial.  Just enough time to get you hooked. But there are tons of possibilities.