2015-07-02 11 views
2

我想在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

我错过了什么? 有没有更好的方法来做到这一点?

+0

我的事情错误是相当的描述,您引用'[System.Windows.Windows.Media.Brush]',但是这应该是'[System.Windows.Media.Brush]'。如果您在此之外需要帮助,可否请编辑包括您的代码在内的问题,以便我们复制该问题? –

+0

谢谢Jan,我没有发现第二个.windows System.Windows.Media.Brush仍然无法加载。 用完整的代码更新了最初的问题。 –

回答

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 
+0

太棒了!这正是我需要的,谢谢。 –