簡體   English   中英

使用XAML和C#設置Windows Phone 8應用程序的計時器

[英]Set Timer For Windows phone 8 App Using XAML With C#

我正在開發windows phone 8游戲應用程序。

我需要在我的應用中添加倒計時時間功能。

比如應用程序啟動時。 計時器值顯示60,59,58 .... 0

當0到達時顯示消息“超時”。

我在谷歌搜索但我不知道。 [可能是我的錯誤沒有以正確的方式搜索]

我嘗試使用以下代碼,它顯示日期和時鍾值,如[3/12/2014 6:22:10 PM]

C#代碼

 public SensorTwo()
    {
        InitializeComponent();
        DispatcherTimer newTimer = new DispatcherTimer();
        newTimer.Interval = TimeSpan.FromSeconds(1);
        newTimer.Tick += OnTimerTick;
        newTimer.Start();
    }

 void OnTimerTick(Object sender, EventArgs args)
    {
        clock.Text = DateTime.Now.ToString();
    }

XAML代碼

<TextBlock Foreground="Red" FontSize="22"  Name="clock" Width="350" Height="50" HorizontalAlignment="Center" VerticalAlignment="Bottom"></TextBlock>

任何一個幫助或指導我完成這個功能......

//make global declaration of a counter variable 


 int counter =60;    
    void OnTimerTick(Object sender, EventArgs args)
       {       
         counter --;
         if(counter<0)
         {
          newTimer.Stop();
          counter=60;
         }
        else
         {
          clock.Text =counter.ToString();
         }
       }

聲明一個應用程序啟動時將重置的計數器

TimeSpan counter = new TimeSpan(0, 0, 60);

這在OnTimerTick()中

counter -= TimeSpan.FromSeconds(1);   
clock.Text = counter.Seconds;

希望這會有所幫助

試試這個

clock.Text = DateTime.Now.ToString(“ss”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM