WPF淡入/淡出仅运行一次

时间:2013-01-30 15:40:57

标签: c# wpf xaml

我有一个带有故事板和触发器的样式。动画效果很好,但只有一次。

我有2个故事板FadeIn和FadeOut。在EnterActions中,我启动FadeIn动画,在ExitActions中启动FadeOut动画。我用

代码启动整个动画
TextBlock.StartFade = true;

当我调试上面的代码时,每次点击StartFade都是False(这是正确的)。

那我该怎么办?

这是XAML中的Style。 FadingTextBlock只是一个带有StartFade依赖属性的自定义TextBlock。

<Style TargetType="{x:Type Controls:FadingTextBlock}">
    <Setter Property="Visibility" Value="Collapsed" />
    <Setter Property="StartFade" Value="False" />
    <Setter Property="Opacity" Value="1.0" />

    <Style.Resources>
        <Storyboard x:Key="FadeIn">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Controls:FadingTextBlock.Opacity)" Storyboard.TargetName="{x:Null}">
                <EasingDoubleKeyFrame KeyTime="0:0:0.0" Value="0.0" />
                <EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="1.0" />
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Controls:FadingTextBlock.Visibility)" Storyboard.TargetName="{x:Null}">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="{x:Static Visibility.Visible}"/>
            </ObjectAnimationUsingKeyFrames>
            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(Controls:FadingTextBlock.StartFade)" Storyboard.TargetName="{x:Null}">
                <DiscreteBooleanKeyFrame KeyTime="0:0:1.5" Value="False" />
            </BooleanAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="FadeOut">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Controls:FadingTextBlock.Opacity)" Storyboard.TargetName="{x:Null}">
                <EasingDoubleKeyFrame KeyTime="0:0:0.0" Value="1.0" />
                <EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="0.0" />
            </DoubleAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Controls:FadingTextBlock.Visibility)" Storyboard.TargetName="{x:Null}">
                <DiscreteObjectKeyFrame KeyTime="0:0:1.5" Value="{x:Static Visibility.Collapsed}" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </Style.Resources>
    <Style.Triggers>
        <Trigger Property="StartFade" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard x:Name="In" Storyboard="{StaticResource FadeIn}" />                    
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard x:Name="Out" Storyboard="{StaticResource FadeOut}" />                    
            </Trigger.ExitActions>
        </Trigger>
    </Style.Triggers>
</Style>

3 个答案:

答案 0 :(得分:3)

您应该使用<StopStoryboard>操作阻止已启动的故事板在退出操作中播放。

    <Trigger Property="StartFade" Value="True">
        <Trigger.EnterActions>
            <StopStoryBoard BeginStoryboardName="Out"/>
            <BeginStoryboard x:Name="In" Storyboard="{StaticResource FadeIn}" />                    
        </Trigger.EnterActions>
        <Trigger.ExitActions>
            <StopStoryBoard BeginStoryboardName="In"/>
            <BeginStoryboard x:Name="Out" Storyboard="{StaticResource FadeOut}" />                    
        </Trigger.ExitActions>
    </Trigger>

答案 1 :(得分:1)

我最终在代码中使用了本地动画。

在Xaml中将TextBlock Opacity设置为0.

// Fading animation for the textblock to show that the settings are updated.
DoubleAnimation fadingAnimation = new DoubleAnimation();
fadingAnimation.From = 0;
fadingAnimation.To = 1;
fadingAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.5));
fadingAnimation.AutoReverse = true;

UpdateMessage.BeginAnimation(TextBlock.OpacityProperty, fadingAnimation);

答案 2 :(得分:0)

我基于@MystyxMac实现了我自己的C#解决方案 将其提取为混合行为,这暴露了挑战..

尝试连续两次显示相同的通知将不起作用,因为将不会调用依赖项属性更改回调..

我通过获取绑定,清除它并再次设置它来克服这个问题。

public class ShowFadingTextBehavior : System.Windows.Interactivity.Behavior<TextBlock>
{

    public static readonly DependencyProperty DurationProperty = DependencyProperty.Register(
      "Duration", typeof(TimeSpan), typeof(ShowFadingTextBehavior), new PropertyMetadata(TimeSpan.FromSeconds(5)));

    public TimeSpan Duration
    {
        get { return (TimeSpan)GetValue(DurationProperty); }
        set { SetValue(DurationProperty, value); }
    }

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text", typeof (string), typeof (ShowFadingTextBehavior), new PropertyMetadata("",OnTextChanged));

    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

        var b = (ShowFadingTextBehavior) d;
        var text = (string) e.NewValue;

        if(string.IsNullOrEmpty(text))
            return;

        b.Show(text);
    }

    private void Show(string text)
    {
        var textBlock = AssociatedObject;

        if(textBlock==null)
            return;

        textBlock.Text = text;

        if(textBlock.Visibility==Visibility.Visible)
            return;

        textBlock.Visibility = Visibility.Visible;

        var a = new DoubleAnimation
        {
            From = 1.0,
            To = 0.0,
            FillBehavior = FillBehavior.Stop,
            BeginTime = TimeSpan.FromSeconds(1),
            Duration = new Duration(Duration)
        };

        var storyboard = new Storyboard();

        storyboard.Children.Add(a);
        Storyboard.SetTarget(a, textBlock);
        Storyboard.SetTargetProperty(a, new PropertyPath(UIElement.OpacityProperty));

        storyboard.Completed += delegate
        {
            textBlock.Visibility = Visibility.Collapsed;
            textBlock.Opacity = 1.0;

            var binding = BindingOperations.GetBinding(this, TextProperty);
            if(binding==null)
                return;

            ClearValue(TextProperty);
            BindingOperations.SetBinding(this, TextProperty, binding);
        };

        storyboard.Begin();

    }

    public string Text
    {
        get { return (string) GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}