Sam Sam - 3 months ago 60
C# Question

UWP blinking text

this might sounds very simple, but I couldn't find a way to do it.

I just want a blinking text in TextBlock. Is there a way to do it easily?

The only way I can think of is to use timer and change the TextBlock foreground manually. Surely there is an easy way to do it. I am just not able to figure it out.

Thanks!

Answer

You could declare a Storyboard animation in your XAML markup which blinks the text forever:

<TextBlock Text="I'm Blinking!">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Resources>
                <Storyboard x:Key="flashAnimation" >
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" AutoReverse="True" Duration="0:0:0.5" RepeatBehavior="Forever" />
                </Storyboard>
            </Style.Resources>
        </Style>
    </TextBlock.Style>
</TextBlock>

This is using my experience from WPF and WinRT XAML, but I'm pretty sure that UWP uses the same Storyboard animations.

Here's Microsoft's handy reference on MSDN: Animations Overview.

Hope this helps!