简体   繁体   English

Powershell GUI 标签更改

[英]Powershell GUI Label change

I am building a GUI in powershell using XAML.我正在使用 XAML 在 powershell 中构建 GUI。 I will be updating the gui label/text with text of what is happening in the program.我将使用程序中发生的事情的文本更新 gui 标签/文本。

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

Example taking this piece of code searching for matches in an array.以这段代码搜索数组中的匹配项为例。 The GUI would have its label say "searching array for matches". GUI 的标签会显示“搜索匹配数组”。 Then when it goes to another part of the code such as doing some math functions.然后当它转到代码的另一部分时,例如执行一些数学函数。 The GUI would say "Doing math functions" So far I can have the GUI update with a button click but I want it to update once that piece of code is being executed. GUI 会说“做数学函数” 到目前为止,我可以通过单击按钮来更新 GUI,但我希望它在执行那段代码后更新。 At the very least have it update on a timer.至少让它在计时器上更新。 I am looking for a way to change the label text automatically.我正在寻找一种自动更改标签文本的方法。 Just a small piece of code of changing a label automatically would help.只需一小段自动更改标签的代码就会有所帮助。

This is actually very simple with a bit of code here.这实际上非常简单,这里有一些代码。 https://foxdeploy.com/functions/ise-snippets/xaml-to-gui/ https://foxdeploy.com/functions/ise-snippets/xaml-to-gui/

You use the function to bind the WFP object to a Powershell one, and update it accordingly.您使用该函数将 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

You would just have your code inside Add_Click, and place the updated text wherever you wanted it to occur.您只需将代码放在 Add_Click 中,然后将更新的文本放置在您希望它出现的任何位置。 On a timer, etc.在计时器等上。

Have you tried something like this?你有没有尝试过这样的事情?

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

You can add that to wherever you would like it to run in your code, multiple times if needed.您可以将它添加到您希望它在代码中运行的任何位置,如果需要,可以多次添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM