如何在wpf中使按钮和背景闪烁

How to make button and background flash in wpf

问题


我遇到的问题是,当用户单击按钮时,我希望按钮开始从 100% 可见闪烁到大约 20% 可见,并且 WPF window 的背景颜色从 #ffffff#d6786a 并且这应该发生直到再次单击按钮。然后动画应该停止。

代码


我已经进行了一些挖掘以找到有人在哪里做过这件事,但我似乎看不到任何东西。我现在的代码如下。

这是我想要在单击时从 100% 可见闪烁到 20% 可见的按钮:

<Button Name="button2" Style="{DynamicResource NoChromeButton}" Visibility="{Binding VisableState, Converter={StaticResource BoolToVis}}" ToolTip="Start Live Stream" Command="{Binding PlayStream}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="5" Height="22" Width="22" >
    <Image Source="../Images/recordicon.png"/>
</Button>

我曾尝试将故事板添加到按钮中,但没有成功:

<Button.Triggers>
  <EventTrigger RoutedEvent="Button.Click">
    <EventTrigger.Actions>
      <BeginStoryboard>
        <Storyboard BeginTime="00:00:00" 
                                RepeatBehavior="Forever" 
                                Storyboard.TargetName="button2" 
                                Storyboard.TargetProperty="(Visibility)">
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger.Actions>
  </EventTrigger>
</Button.Triggers>

我是 WPF 的新手,所以进入动画对我来说比较困难。如果有人能在这个舞台上给我一点帮助那就太好了。

在资源中定义动画,例如像这样

<Window x:Name="window" x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Storyboard x:Key="FlashButton" RepeatBehavior="Forever">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="button">
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.2"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="ChangeColor">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid">
            <EasingColorKeyFrame KeyTime="0:0:1" Value="Red"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="ChangeColor2">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid">
            <EasingColorKeyFrame KeyTime="0:0:1" Value="White"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>
<Grid x:Name="grid" Background="White">
    <Button x:Name="button" Content="Button" Margin="10,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75" Click="button_Click"/>

</Grid>

并为按钮点击创建处理程序

        private bool isRunning;
    private void button_Click(object sender, RoutedEventArgs e)
    {
        var flashButton = FindResource("FlashButton") as Storyboard;
        var changeColor = FindResource("ChangeColor") as Storyboard;
        var changeColor2 = FindResource("ChangeColor2") as Storyboard;

        if (isRunning)
        {
            flashButton.Stop();
            changeColor2.Begin();
            isRunning = false;
        }
        else
        {
            flashButton.Begin();
            changeColor.Begin();
            isRunning = true;
        }
    }

要更改 "visibility to around 20%",您必须 动画 Opacity property (double type from 0.0 to 1.0) not Visibility (enum with 3 values, see msdn).

如果你可以使用 ToggleButton(因为它很好的 CheckedUnckecked 路由事件),这个目标很容易实现:

<ToggleButton>
    <ToggleButton.Triggers>
        <EventTrigger RoutedEvent="ToggleButton.Checked">
            <BeginStoryboard x:Name="storyboard">
                <Storyboard RepeatBehavior="Forever"
                            AutoReverse="True">
                    <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                     To="0.2"
                                     Duration="0:0:1" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="ToggleButton.Unchecked">
            <StopStoryboard BeginStoryboardName="storyboard" />
        </EventTrigger>
    </ToggleButton.Triggers>
</ToggleButton>