Bra*_*ell 6 wpf 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)
这个错误就是我所说的"因为我说的错误",这意味着它的确如此.
您无法在Powershell.exe中加载XAML,因为它是STA.而"很多"组件需要这个.我的很多,我的意思是WPF.
幸运的是,每个PowerShell主机都支持STA
但是,在PowerShell中编写UI的方法要简单得多.你应该看看showui.codeplex.com.
希望这可以帮助,
| 归档时间: |
|
| 查看次数: |
8430 次 |
| 最近记录: |