Powershell WPF表单将文本框背景更改为图像

时间:2015-07-02 07:13:23

标签: wpf xaml powershell background background-image

我试图在Powershell中将文本框的背景设为图像。 我正在使用来自http://blogs.technet.com/b/heyscriptingguy/archive/2014/08/01/i-39-ve-got-a-powershell-secret-adding-a-gui-to-scripts.aspx的xaml转换器 加载xaml表单:

<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

我错过了什么? 有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

这是一个适合我的例子。这有点令人费解,但无论如何,那是WPF。

您必须定义 uri (图片路径)。基于此,您加载 BitmapImage 并使用它来创建 ImageBrush 。这可以反过来设置为文本框的背景。

Add-Type –assemblyName WindowsBase
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName PresentationFramework

[xml]$xaml = @' 
<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>
'@

$reader=(New-Object Xml.XmlNodeReader $xaml) 
$MainForm=[Windows.Markup.XamlReader]::Load( $reader ) 

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $MainForm.FindName($_.Name) -Scope Global}

$btnGo.add_Click({
    $txtOut.text = ""
    $uri = new-object system.uri("d:\documents\MAP - Discovery.png")
    $imagesource = new-object System.Windows.Media.Imaging.BitmapImage $uri 
    $imagebrush = new-object System.Windows.Media.ImageBrush  $imagesource
    $txtOut.background = $imagebrush
})

$MainForm.ShowDialog() | out-null