我是WP开发的新手,目前正在玩动画。我现在要做的是设置TextBlock的Text属性的动画。
出于培训目的,我正在开发一个简单的温度转换应用程序,屏幕上有一个很大的数字(温度),我想逐渐增加或降低,直到它达到另一个值(例如,通过显示中间的每个数字,从10到24)。
我试着在text属性上使用故事板,但我认为它不起作用。然后,我尝试将属性逐个设置为每个值(对于循环),但视图没有足够定期地刷新,应用程序会阻塞,直到循环结束并只显示最后一个值。我不知道我想做的事情是否可能(我希望这并不罕见,对吧?)而且我没有其他想法来得到我想要的结果。有人知道吗?
感谢:)
您可以使用DispatcherTimer,并在其Tick
事件处理程序中更新TextBlock的Text属性:
private readonly DispatcherTimer timer = new DispatcherTimer();
private int currentValue;
private int endValue;
public MainPage()
{
...
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += TimerTick;
}
private void TimerTick(object sender, object e)
{
currentValue++;
textBlock.Text = currentValue.ToString();
if (currentValue >= endValue)
{
timer.Stop();
}
}
private void AnimateText(int start, int end)
{
currentValue = start;
endValue = end;
textBlock.Text = currentValue.ToString();
if (currentValue < endValue)
{
timer.Start();
}
}