如何使用 Powershell / XAML 创建一个闪烁的按钮?
How can I create a blinking button with Powershell / XAML?
以下代码更改鼠标悬停时按钮的颜色。
但我想每 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)
}
如果你想在没有用户交互的情况下进行更改,你可以尝试使用像Start-Job
这样的线程
您应该可以使用 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'
}
})
以下代码更改鼠标悬停时按钮的颜色。
但我想每 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)
}
如果你想在没有用户交互的情况下进行更改,你可以尝试使用像Start-Job
您应该可以使用 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'
}
})