簡體   English   中英

Powershell GUI 標簽更改

[英]Powershell GUI Label change

我正在使用 XAML 在 powershell 中構建 GUI。 我將使用程序中發生的事情的文本更新 gui 標簽/文本。

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

以這段代碼搜索數組中的匹配項為例。 GUI 的標簽會顯示“搜索匹配數組”。 然后當它轉到代碼的另一部分時,例如執行一些數學函數。 GUI 會說“做數學函數” 到目前為止,我可以通過單擊按鈕來更新 GUI,但我希望它在執行那段代碼后更新。 至少讓它在計時器上更新。 我正在尋找一種自動更改標簽文本的方法。 只需一小段自動更改標簽的代碼就會有所幫助。

這實際上非常簡單,這里有一些代碼。 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 中,然后將更新的文本放置在您希望它出現的任何位置。 在計時器等上。

你有沒有嘗試過這樣的事情?

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

您可以將它添加到您希望它在代碼中運行的任何位置,如果需要,可以多次添加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM