Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

WPF from PowerShell, the making of the WPF WMI Explorer Part2

In this second part in this series ( the 5th about WPF ) a bit more about creating the interfacing part, and accessing it from the PowerShell code

As you can see in the last post in Jaykul's ongoing series about WPF, WPF From PowerShell - Select-Grid and e.g. in my former post about More things to do with XAML from PowerShell with the graphic examples I did steal, there lot's a cool things you can do with WPF Also note in this post how he completely creates the WPF form from code here without using any embedded XAML), as I'm just learning WPF my self, probably I will not show the best way to do things in WPF, but just how I got it working ;-)

Jaykul in the post before made a note about the size of the former and new WPF WMI explorer, where he wonders if this is just by using WPF

which he rewrites his PowerShell WMI Explorer to be WPF based — it seems to not only run faster, but be based on less code (of course, that might just be because he’s gained experience since the first version).

let's show the difference is size between the 2 versions of the WMI explorer in a WPF form with his own script :

image

Ok I do not have every little thing implemented, but also I added the filtering capabilities, in the Windows forms version most of the code is code to build the form (translated from C# Visual Studio prototype ) whilst in the WPF version only a small part (60 - 70 lines) is used to build the form in XAML, so certainly a lot gain in code size by just switching to WPF, but most important it's much easier to maintain.

Note that you do not NEED to use XAML you can also build up the form from code as Jaykul did show in Select-Grid, and as when using the Windows.Forms namespace.

we can leverage other peoples skills on this ( any volunteers to give my WMI explorer XAML form code a fresh up ? ) , and learning by looking at the XAML code generated by different editors and written by other people, for example, by looking at Jaykul's code I found some better ways to do some things in my PowerShell WMI Explorer WPF Edition first beta for the next version.But let's go on with this post by building a simple form and attach to the controls and events from PowerShell. In the rest of the post you can see how easy this is as the form code and script logic are separated.

So let's add a Button (that we call btnGreet) and a RichTextbox to the form in the editor (In this case VS2008 again)

image

When we add this XAML code to the PowerShell WPF template script it looks like this :

Add-Type -AssemblyName presentationframework
[xml]$XAML = @'
<Window
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="154" Width="300">
    <Grid>
        <Button Height="23" Margin="9,8,0,0" Name="btnGreet" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75">Greet</Button>
        <TextBox Margin="5,34,5,5" Name="textBox" />
    </Grid>
</Window>
'@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )

$Form.ShowDialog() | out-null

but now we need to be able to add an action to the button, to do this we first should be able to get access to the Button.

$btnGreet = $form.FindName('btnGreet')

In Graphical PowerShell we can run only part of the script by selecting it and pressing F5 (so we can leave out starting the Form and exploring the Button

image

As we also need access to the RichTextbox for the output, we "find" that control also and add it to a variable

$rtbOutput = $form.FindName('rtbOutput') 

now we have access to the controls we need we can add an action.

to the button by providing a Scriptblock as a delegate to the Buttons Click handler like this :

$btnGreet.add_click({ $rtbOutput.AppendText("Hello PoSHy World !`n") })

and we have added an action to the button that outputs some text to the RichtextBox

image 

Note that now we can make the XAML as fancy as we want, as long as we have a button names "btnGreet" and a RichtextBox called rtbOutput, our script keeps working.hence it is possible to let a designer redesign the interface, as long as those objects remain accessible

, and as also Jeffrey Snover mentions in The Future Is Here Today also about Jakul's select-grid post ,

Now image that have a graphics friend and you ask them to replace the WPF with something better looking from one of their professional XAML tools (e.g. Blend) and you get to use that XAML by just referring to it.

Enjoy,

Greetings /\/\o\/\/

Published Wednesday, May 21, 2008 1:38 PM by MoW
Filed under: ,

Comments

# TechProsaic - I write about great software, Internet technology, cool gadgets, and The Next Big Thing. &raquo; PowerShell Plus now supports Single Threaded Apartment mode

# Edit-Content - ???????????????????? ?? WPF &laquo; PowerShell ?? ???????????? ??????????????

Anonymous comments are disabled