以下代碼在滑鼠懸停時更改按鈕的顏色。
但我想在沒有滑鼠懸停的情況下每 500 毫秒更改一次顏色。
我需要為此做些什么?
非常感謝您的幫助!
老虎
# Load the window from XAML
$Window = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml))
# Custom function to add a button
Function Add-Button {
Param($Content)
$Button = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $ButtonXaml))
$ButtonText = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $ButtonTextXaml))
$ButtonText.Text = "$Content"
$Button.Content = $ButtonText
$Button.Content.FontSize = "16"
$Button.Content.FontWeight = "Bold"
$Button.Content.Background = "#E6E6E6" # LightGray
$Button.Add_MouseEnter({
$This.Content.Background = "#00A387"
$This.Content.Foreground = "white"
})
$Button.Add_MouseLeave({
$This.Content.Background = "#D8D8D8"
$This.Content.Foreground = "black"
})
$Button.Add_Click({
New-Variable -Name WPFMessageBoxOutput -Value $($This.Content.Text) -Option ReadOnly -Scope Script -Force
$Window.Close()
})
$Window.FindName('ButtonHost').AddChild($Button)
}
uj5u.com熱心網友回復:
您應該可以使用 Timer 控制元件來做到這一點:
Add-Type -AssemblyName PresentationFramework,PresentationCore,WindowsBase
在創建時為您的按鈕命名,以便您可以使用$Window.FindName('FlashingButton')
$Button.Name = 'FlashingButton'
使用計時器讓背景顏色每 500 毫秒更改一次
$Timer = [System.Windows.Threading.DispatcherTimer]::new()
$Timer.Interval = [timespan]::FromMilliseconds(500)
# in the Tick event, toggle the colors
$Timer.Add_Tick({
# find the button control
$button = $Window.FindName('FlashingButton')
if ($button.Content.Background -eq '#00A387') {
$button.Content.Background = '#D8D8D8'
$button.Content.Foreground = 'black'
}
else {
$button.Content.Background = '#00A387'
$button.Content.Foreground = 'white'
}
})
$Timer.Start()
全部完成后,停止計時器
$Timer.Stop()
為了不必在每次觸發 Tick 事件時都找到按鈕,請在前面存盤對它的參考,并在 Tick({..}) 中將其與$script:范圍一起使用:
# store a reference to the button in a variable
$FlashButton = $Window.FindName('FlashingButton')
# in the Tick event, toggle the colors
$Timer.Add_Tick({
if ($script:FlashButton.Content.Background -eq '#00A387') {
$script:FlashButton.Content.Background = '#D8D8D8'
$script:FlashButton.Content.Foreground = 'black'
}
else {
$script:FlashButton.Content.Background = '#00A387'
$script:FlashButton.Content.Foreground = 'white'
}
})
uj5u.com熱心網友回復:
如果您想在沒有用戶互動的情況下進行更改,可以嘗試使用執行緒之類的Start-Job
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434580.html
上一篇:無法將IIS結果匯出到CSV
