2015-04-03 39 views
0

我對WP的開發頗爲陌生,目前正在使用動畫。 我現在試圖做的是動畫TextBlock的Text屬性。對TextBlock的Text屬性進行動畫處理

爲了培訓的目的,我開發了一個簡單的溫度轉換應用程序,屏幕上有一個很大的數字(溫度),我想逐漸增加或減少,直到達到另一個值(例如從10到24通過顯示中間的每個數字)。

我試圖在文本屬性上使用故事板,但因爲我認爲它不起作用。然後我嘗試將屬性設置爲逐個值(for循環),但視圖不會定期刷新,應用程序會阻塞,直到循環結束並僅顯示最後一個值。 我不知道我想做什麼是可能的(我希望它不是那麼罕見,對嗎?),我沒有其他想法來得到我想要的結果。 有沒有人有這個想法?

謝謝:)

+3

嘗試[DispatcherTimer(https://msdn.microsoft.com /en-us/library/windows/apps/windows.ui.xaml.dispatchertimer)。 – Clemens 2015-04-03 12:18:47

+0

非常完美,謝謝!請您再說一遍,但作爲迴應而不是評論,所以我可以將它記下來並將其標記爲答案? – Jeahel 2015-04-03 13:17:05

回答

1

您可以使用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(); 
    } 
} 
+0

非常感謝你,正是我需要:) – Jeahel 2015-04-03 13:43:36

相關問題