Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to load XAML in powershell

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>
like image 845
Brandon Risell Avatar asked Jun 30 '11 21:06

Brandon Risell


2 Answers

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,

like image 196
Start-Automating Avatar answered Oct 11 '22 05:10

Start-Automating


@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.

like image 40
Doug Finke Avatar answered Oct 11 '22 04:10

Doug Finke