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

Powershell WPF表单将文本框背景更改为图像,wpf,xaml,powershell,background,background-image,Wpf,Xaml,Powershell,Background,Background Image,我正在尝试将文本框的背景设置为Powershell中的图像。 我正在使用来自的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">

我正在尝试将文本框的背景设置为Powershell中的图像。 我正在使用来自的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>
我得到一个错误:

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
新对象:未找到构造函数。找不到类型System.Windows.Media.Brush的适当构造函数。
在UAT.ps1:40字符:24

+$image=newobject下面是一个适合我的示例。这有点复杂,但对我来说,这就是WPF

您必须定义一个uri(图像路径)。在此基础上加载位图图像,并使用它创建图像笔刷。这可以反过来设置为文本框的背景

添加类型–assemblyName WindowsBase
添加类型–assemblyName PresentationCore
添加类型–assemblyName PresentationFramework
[xml]$xaml=@'
'@
$reader=(新对象Xml.XmlNodeReader$xaml)
$MainForm=[Windows.Markup.XamlReader]::加载($reader)
$xaml.SelectNodes(“//*[@Name]”)|%{Set Variable-Name($\.Name)-Value$MainForm.FindName($\.Name)-Scope Global}
$btnGo.add\u单击({
$txtOut.text=“”
$uri=newobjectsystem.uri(“d:\documents\MAP-Discovery.png”)
$imagesource=新对象System.Windows.Media.Imaging.BitmapImage$uri
$imagebrush=新对象System.Windows.Media.imagebrush$imagesource
$txtOut.background=$imagebrush
})
$MainForm.ShowDialog()|输出空值

我认为错误是相当描述性的,您正在引用
[System.Windows.Windows.Media.Brush]
,但这应该是
[System.Windows.Media.Brush]
。如果您还需要其他帮助,请编辑包含代码的问题,以便我们可以复制该问题?谢谢Jan,我还没有发现第二个问题。尽管如此,windows System.windows.Media.Brush仍然无法加载。用完整代码更新了初始问题。
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