小编Bra*_*ell的帖子

无法在PowerShell中加载XAML

我有一个非常基本的PowerShell脚本,可以加载xaml布局.它抛出了这个错误:

用"1"参数调用"Load"的异常:
"无法创建
程序集'PresentationFramework,Version = 3.0.0.0,
Culture = neutral,PublicKeyToken = 31bf3856ad364e35'中定义的'Window'实例.
调用线程必须是STA,因为很多UI组件都需要这个."

我已经多次查看了脚本和xaml文件,但是我没有看到问题出在哪里,并且我无法完全理解我所获得的错误也无济于事.任何帮助将非常感激.谢谢.

Powershell脚本:

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()
Run Code Online (Sandbox Code Playgroud)

Xaml文件:

<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>
Run Code Online (Sandbox Code Playgroud)

wpf powershell xaml

6
推荐指数
1
解决办法
8430
查看次数

标签 统计

powershell ×1

wpf ×1

xaml ×1