gpt4 book ai didi

c# - 单击后更改 wpf C# 中按钮的颜色并在 2 分钟后保留原始颜色

转载 作者:行者123 更新时间:2023-12-04 00:40:10 45 4
gpt4 key购买 nike

我正在使用这个代码

        Hello.Background = System.Windows.Media.Brushes.Blue;
var dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromSeconds(61);
TimeSpan span = new TimeSpan(0,1,0);
dispatcherTimer.Start();
dispatcherTimer.Tick += delegate
{

if (dispatcherTimer.Interval > span)
{
Hello.Background = System.Windows.Media.Brushes.Red;
dispatcherTimer.Stop();
}
};

但是按钮一直淡入淡出。我希望颜色保持不变

C#

            private void Button_Click(object sender, RoutedEventArgs e)
{
Hello.Background = System.Windows.Media.Brushes.Blue;
var dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromSeconds(61);
TimeSpan span = new TimeSpan(0,1,0);
dispatcherTimer.Start();
dispatcherTimer.Tick += delegate
{

if (dispatcherTimer.Interval > span)
{
Hello.Background = System.Windows.Media.Brushes.Red;
dispatcherTimer.Stop();
}
};


}

Xaml

<Button Name="Hello" Content="Hello" Background="White"  Foreground="Black " Click="Button_Click">
</Button>

最佳答案

您可以创建一个 Style 并使用一个 Trigger 来启动一个带有 ColorAnimationsStoryboard

例子:

<Style x:Key="AnimatedButton" TargetType="Button">
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetProperty="Background.Color">
<ColorAnimation To="Blue" Duration="0:0:4" />
<ColorAnimation To="Red" BeginTime="0:1:52" Duration="0:0:4" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>

关于c# - 单击后更改 wpf C# 中按钮的颜色并在 2 分钟后保留原始颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19674662/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com