本文介绍了WPF 淡入/淡出仅运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Style with a Storyboard and Triggers. The animation work nicely, but only once.

我有 2 个故事板淡入和淡出.在 EnterActions 中我启动 FadeIn 动画,在 ExitActions 中启动 FadeOut 动画.我用

I have 2 Storyboards FadeIn and FadeOut. In the EnterActions I start the FadeIn animation and in the ExitActions the FadeOut animation. I start the whole animation in code with

TextBlock.StartFade = true;

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

When i debug the above code, with every hit StartFade is False (which is correct).

那我做错了什么?

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

Here is the Style in XAML. FadingTextBlock is just a custom TextBlock with a StartFade dependency property.

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

推荐答案

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

I ended up using a local animation in code.

在 Xaml 中将 TextBlock Opacity 设置为 0.

Set the TextBlock Opacity to 0 in Xaml.

// 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);

这篇关于WPF 淡入/淡出仅运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:10