Powershell GUI标签更改

时间:2017-09-28 17:22:45

标签: wpf powershell xaml user-interface data-binding

我正在使用XAML在powershell中构建GUI。我将使用程序中发生的事情的文本更新gui标签/文本。

for($i in $array){
  if $i -eq "item"{
   add item to matcharray
  }
}

使用这段代码搜索数组中的匹配项的示例。 GUI会有标签说"搜索数组匹配"。然后当它转到代码的另一部分,比如做一些数学函数。 GUI会说"做数学函数"到目前为止,我可以通过单击按钮进行GUI更新,但是我希望在执行该代码时更新它。至少让它更新一个计时器。我正在寻找一种自动更改标签文本的方法。只需一小段自动更改标签的代码就可以了。

2 个答案:

答案 0 :(得分:1)

这实际上非常简单,这里有一些代码。 https://foxdeploy.com/functions/ise-snippets/xaml-to-gui/

您可以使用该函数将WFP对象绑定到Powershell对象,并相应地更新它。

$inputXML = @"
XAML
"@       

$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try {
    $Form=[Windows.Markup.XamlReader]::Load( $reader )
} catch { 
    Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
}

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

Function Get-FormVariables
{
    if ($global:ReadmeDisplay -ne $true) {
        Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow; $global:ReadmeDisplay=$true
    }
    write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
    get-variable WPF*
}

Get-FormVariables

#Updates the XAML thing named "label1"
$WPFlabel1.Text = "This is the starting value!"

$WPFbutton1.Add_Click({
$WPFlabel1.Text = "This is the updated value!"
})
$Form.ShowDialog() | out-null

您只需将代码放在Add_Click中,然后将更新的文本放在您想要的位置。在计时器等

答案 1 :(得分:0)

你尝试过这样的事吗?

$Label.Text = "Whatever you want your label to say here"

如果需要,您可以将其添加到您希望在代码中运行的任何位置。

相关问题