6

I have a very basic powershell script that loads a xaml layout. It is throwing this error:

Exception calling "Load" with "1" argument(s):
"cannot create instance of 'Window' defined in
assembly 'PresentationFramework, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
The calling thread must be STA, because many UI components require this. "

I've gone over the script and xaml files several times, but I'm not seeing where the problem is, and it doesn't help that I don't fully understand what the error I'm getting means either. Any help would be much appreciated. Thank You.

Powershell script:

Add-Type -AssemblyName presentationframework
$xaml = [xml](Get-Content MainWindow.xaml)
$reader = New-Object System.Xml.XmlNodeReader $xaml
$form = [Windows.Markup.XamlReader]::Load($reader)
$button = $form.FindName('testButton')
$button.Add_Click({
    $s = $form.FindName('testBox').Text;
    $form.FindName('testLabel').Content = $s;
})
$form.ShowDialog()

Xaml file:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot">
        <TextBox x:Name="testBox" HorizontalAlignment="Left"
                 Margin="26.782,48.626,0,0" TextWrapping="Wrap"
                 Text="New label text" VerticalAlignment="Top" Width="130.22"/>
        <Label x:Name="testLabel" Content="User Search" HorizontalAlignment="Left"
                 Margin="20.666,22.666,0,0" VerticalAlignment="Top"/>
        <Button x:Name="testButton" Content="Change It!" HorizontalAlignment="Left"
                 Margin="26.782,85,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
Brandon Risell
  • 149
  • 2
  • 9

3 Answers3

7

This error is what I call the "because I said so error", and it means exactly what it says.

You can't load XAML in Powershell.exe because it's STA. And "many" components require this. My many, I mean WPF.

Luckily, every PowerShell host supports STA

  • powershell.exe -sta (turns on STA mode)
  • Powershell Integrated Scripting Environment (STA by default)
  • PowerGUI (STA by default)
  • PowerShellPlus (hold shift in startup to turn on STA)
  • PrimalScript (it's in a checkbox along the top bar)
  • PowerSE / PowerWF (STA coming soon)

There are much easier ways to write UI in PowerShell though. You should check out showui.codeplex.com.

Hope this helps,

Start-Automating
  • 8,067
  • 2
  • 28
  • 47
2

@BrandonRisell, check out ShowUI a PowerShell module to help build WPF user interfaces in script.

ShowUI makes the complicated world of WPF easy to use in PowerShell. You can use ShowUI to write simple WPF gadgets, quick front ends for your scripts, components, and full applications.

Doug Finke
  • 6,675
  • 1
  • 31
  • 47
-1

It means that your main() must be annotated with [STAThread] and I'm not sure it's possible in powershell.
For more info about STA vs. MTA see here.

Community
  • 1
  • 1
the_drow
  • 18,571
  • 25
  • 126
  • 193
  • 3
    Start PowerShell with `powershell -Sta`. – Joey Jun 30 '11 at 21:50
  • @the_drow Thank you for the link to the post, I didn't realize the difference in threads @Joey Do you know of any other more elegant solutions or is that about it? – Brandon Risell Jun 30 '11 at 22:12
  • @BrandonRisell: If you think the answer is useful instead of commenting thank you, please upvote. It's the way things are done here. – the_drow Jul 01 '11 at 00:43
  • @Joey: I believe that this should not be a comment but be the accepted answer. – the_drow Jul 01 '11 at 00:44
  • That was more a response to your »I'm not sure it's possible in PowerShell«. For a proper potentially accepted answer, [James' answer](http://stackoverflow.com/questions/6541313/unable-to-load-xaml-in-powershell/6544511#6544511) is probably even better. – Joey Jul 01 '11 at 07:48
  • I can't upvote yet, only 11 rep :(. I definitely would if I could, I'm sorry. – Brandon Risell Jul 01 '11 at 21:23