Powershell and Events in WPF

I have seen this a few times in my Google referrals. People want to know how to how to add events to PresentationFramework controls in Powershell.
I’m sorry to tell you this, but, as of Powershell v1.0, you can’t use script blocks generated in Powershell as event handlers in any STA style user interface, including WPF. This has to do with how Powershell currently handles threading. That’s why I had to make three really long scripts just to show a single window.
If you really want to be able to add event handlers to WPF controls have a look at my New-CAssembly script. That will make it easier to include compiled code, such as event handlers, into a Powershell script. Then, look at my Library-PresentationInterface script. That will help you pack a custom user interface into a script as XAML.
Alternatively you may look at a few of Team Powershell’s recent posts about controlling WPF through Powershell v2.0 CTP 2. They added an STA flag to CTP 2. This helps to resolve some of the threading issues in regards to controlling STA style user interfaces.

Advertisements

~ by lunaticexperimentalist on May 27, 2008.

2 Responses to “Powershell and Events in WPF”

  1. You can use PowerBoots in v1 which supports events too :)

  2. Heh. This post is extremely retro. Use newer stuff like PowerBoots as Joel suggests. Library-PresentationInterface is really hard to use. It’s better to just update to Powershell version 2.0 and use the sta flag.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: