我想在Powershell中使文本框的背景成为图像。 即时通讯使用的XAML转换器从http://blogs.technet.com/b/heyscriptingguy/archive/2014/08/01/i-39-ve-got-a-powershell-secret-adding-a-gui-to-scripts.aspx 加载XAML形式:Powershell WPF将文本框背景变为图像
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="UAT" Height="300" Width="300">
<Grid>
<Button Name="btnGo" Content="Go" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="207,240,0,0"/>
<TextBox Name="txtOut" HorizontalAlignment="Left" Height="233" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="272" Margin="10,0,0,0"/>
</Grid>
</Window>
的名为.ps1文件:
Add-Type –assemblyName PresentationFramework
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName WindowsBase
.\loadDialog.ps1 -XamlPath '.\UAT.xaml' 2>> Errors.txt
$txtOut.text = ""
$btnGo.add_Click({
$txtOut.text = ""
$image = New-Object System.Windows.Media.Brush
$image = ".\bg1.png" #.Source
$txtOut.background = $image
})
$xamGUI.ShowDialog() | out-null 2>> Errors.txt
我得到一个错误:
New-Object : Constructor not found. Cannot find an appropriate constructor for type System.Windows.Media.Brush.
At UAT.ps1:40 char:24
+ $image = New-Object <<<< System.Windows.Media.Brush
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand
Exception setting "Background": "Cannot convert value ".\bg1.png" to type "System.Windows.Media.Brush". Error: "Token is not valid.""
At UAT.ps1:44 char:13
+ $txtOut. <<<< background = $image
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
我已经正在加载: PresentationFramework, PresentationCore, WindowsBase
我错过了什么? 有没有更好的方法来做到这一点?
我的事情错误是相当的描述,您引用'[System.Windows.Windows.Media.Brush]',但是这应该是'[System.Windows.Media.Brush]'。如果您在此之外需要帮助,可否请编辑包括您的代码在内的问题,以便我们复制该问题? –
谢谢Jan,我没有发现第二个.windows System.Windows.Media.Brush仍然无法加载。 用完整的代码更新了最初的问题。 –