Reputation: 1908
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!
Upvotes: 3
Views: 2459
Reputation: 3180
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!
Upvotes: 8
Reputation: 2679
From code behind (C#) you can do it for example with code like this:
Storyboard storyboard = new Storyboard();
storyboard.Duration = new Duration(TimeSpan.FromSeconds(10.0));
DoubleAnimation opacityAnimation = new DoubleAnimation()
{
From = 1.0,
To = 0.0,
BeginTime = TimeSpan.FromSeconds(5.0),
Duration = new Duration(TimeSpan.FromSeconds(5.0))
};
Storyboard.SetTarget(opacityAnimation, txtBlink);
Storyboard.SetTargetProperty(opacityAnimation, "Opacity");
storyboard.Children.Add(opacityAnimation);
storyboard.RepeatBehavior = RepeatBehavior.Forever;
storyboard.AutoReverse = true;
storyboard.Begin();
Assuming you have a textblock:
<TextBlock x:Name="txtBlink" FontSize="32">Some text</TextBlock>
Upvotes: 6
Reputation: 7
For blinking text, I think timer is the easiest approach. (Though i understand you have the thought of using a timer & looking for an alternate, tier should actually serve the purpose) Might be something like below can help you.
Timer timer = new Timer();
public void Blinker()
{
timer.Interval = 500;
timer.Start();
timer.Tick += new EventHandler(timer_tick);
}
void timer_tick(object sender, EventArgs e)
{
if(label3.ForeColor = System.Drawing.Color.Green)
{
label3.ForeColor = System.Drawing.Color.White;
}
else
{
label3.ForeColor = System.Drawing.Color.Green;
}
}
public void StopBlinker()
{
timer.Stop();
}
Now you can call the blinker method appropriately to make your text blink.
Upvotes: 0