简体   繁体   中英

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>

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,

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM