【问题标题】:ColorAnimation stuck on color when button is pressed repeatedly重复按下按钮时,ColorAnimation 卡在颜色上
【发布时间】:2013-07-10 09:56:50
【问题描述】:

我有一个自定义的button-style 和一个ColorAnimation

这很好用,但是当反复按下多次时,它会停留在目标颜色上。

<Style TargetType="Button" x:Key="mainButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                    <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation 
                                    Duration="0:0:0.10" 
                                    Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" 
                                    To="Red"
                                    AutoReverse="True"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我该如何解决这个问题?

【问题讨论】:

    标签: c# wpf xaml button coloranimation


    【解决方案1】:

    更新

    是的,如果您无法删除Trigger.ExitActions 中的Storyboard,那么您确实必须自己解决From 的中间启动问题。

    但是,指定硬编码的From 不是唯一的解决方案。您可以让动画在启动时自行重置为基础颜色。

    这样做的好处是不指定From,您可以在以后的更新中少做一件事情来跟踪。

    <Storyboard AutoReverse="True">
    
      <!-- By not specifying a To or From we pretty much reset the property to un-animated state(Exactly what the hard-coded from does) -->
      <ColorAnimation Duration="0:0:0"
                      Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" />
    
      <!-- This part is same as original time to kick into new Foreground as desired -->
      <ColorAnimation Duration="0:0:1.5"
                      Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"
                      To="Red" />
    </Storyboard>
    

    【讨论】:

    • 我试过这个,但这让我的StoryBoard 几乎不可见。它闪烁.. 我增加了duration 但它保持不变,Vanlalhriata's 答案解决了它。
    • @DeMama 哦,是的,这是因为 RemoveStoryboard 被调用,即使动画未完成,触发器也会退出。然而,指定硬编码的From 并不是唯一的解决方案。我已经用一种替代方法更新了我的答案,该方法让动画通过在动画开始时不指定 From 或 To 值来自行重置。这样做的唯一真正好处是不指定硬编码的From,并跟踪它以备将来对控件Style的所有更新@
    【解决方案2】:

    您尚未在ColorAnimation 上设置From 属性。因此,当您在动画中间按下按钮时,Storyboard 会将当前的Foreground 颜色值作为其From,这就是动画反转回的颜色。

    现在,当您反复按下按钮时,From 颜色会越来越接近红色,给人的印象是颜色卡在红色上。


    更新:

    这个答案只指出了问题。请参阅 Viv 的答案以获得优雅的解决方案

    【讨论】:

    • 这确实是正在发生的事情。首先是淡淡的white-reddish 颜色,然后是粉红色。最终它变红了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2019-10-13
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多