3

我正在使用此代码

        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>
4

4 回答 4

4

您可以只创建 a并Style使用 aTrigger开始 aStoryboardColorAnimations

例子:

<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>
于 2013-10-30T06:12:31.700 回答
1

现在刚刚尝试过.....但是这是背后的代码

XAML 代码:

   <Button  Content="Button"  x:Name="MyButton" Height="23" HorizontalAlignment="Left" 
    Margin="94,128,0,0"  VerticalAlignment="Top" Width="75"/>

Cs 文件

    private void StartAnimation()
    {

        Color fromRGB= Color.FromRgb(255, 255, 255); ;
        Color ToRGB= Color.FromRgb(255, 0, 0);

        SolidColorBrush myBrush = new SolidColorBrush();
        myBrush.Color = Colors.Black;
        ColorAnimation myAnimation = new ColorAnimation();
        myAnimation.From = fromRGB;
        myAnimation.To = ToRGB;
        myAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(120000));
        myAnimation.AutoReverse = true;

        myBrush.BeginAnimation(SolidColorBrush.ColorProperty, myAnimation );

        MyButton.Background = myBrush;
    }

您可以在调用事件时更改颜色,然后调用动画。

于 2013-10-30T06:05:41.547 回答
0

只是为了真正展示一种不同的方法......如果您使用的是 MVVM,您可以将按钮颜色绑定到 ViewModel 上的属性,然后单击它,运行 2 分钟 background/timer 。2分钟完成后,它会将颜色更改为另一种颜色。

涉及的 xaml 并不多,我确实喜欢这里的其他一些解决方案 :)

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    Thread.Sleep(2000); // two second
        ButtonColorPropertyName= Colors.Red;
}

(假设您ButtonColorPropertyName在 ViewModel 中有 或任何您想命名的名称)

于 2013-10-30T06:16:40.503 回答
0

您可以尝试使用在 Blend 中创建的简单 StoryBoard,然后将其应用于按钮/样式,如下所示:

<Button Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" >
        <Button.Background>
            <SolidColorBrush x:Name="MySolidColorBrush" Color="Brown" />
        </Button.Background>
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation 
                            Storyboard.TargetName="MySolidColorBrush"
                            Storyboard.TargetProperty="Color"
                            From="Red" To="Yellow" Duration="0:0:0" RepeatBehavior="1x"  />
                        <ColorAnimation 
                            Storyboard.TargetName="MySolidColorBrush"
                            Storyboard.TargetProperty="Color"
                            From="Yellow" To="Blue" Duration="0:0:0" RepeatBehavior="1x" BeginTime="0:0:10"  />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            </Button.Triggers>
    </Button>
于 2013-10-30T06:02:48.997 回答