问题
I'm beginner in Metro Style Developing, I tried to make a small game that's need a timer, countdown from 10 to 0. But at this Visual Studio there is no Timer Component, and DispatcherTimer Not mapped to an xmlns.
I Tried to use TimeSpan, that have this field "TicksPerSecond" But this example dosen't help me to make my countdown timer : http://msdn.microsoft.com/en-us/library/system.timespan.tickspersecond.aspx
TimeSpan also have this method "FromSeconds", and i Can't also use this one for my countdown timer : http://msdn.microsoft.com/en-us/library/system.timespan.fromseconds.aspx
also I Read this question at stackoverflow : How to Implement a timer in Metro Style App but this isn't useful.
what can I do for this countdown timer?
回答1:
There is a CountdownControl in the WinRT XAML Toolkit that could help you too - it is basically using an animation that takes 1s between events. Another option would be to use the async/await keywords - you can basically say await Task.Delay(1000) to wait for a second and call it in a loop to count down seconds. This would also be portable.
回答2:
I'm not sure what do you mean by xmlns, do you want to use DispatcherTimer in XAML? Ok, it is not possible, but it is not a big deal, just use it from your code.
In my application, I'm intensively using Reactive Extensions. The Rx v2.0 Beta supports winrt, so the timer issue could be solved in the following way (again in the code behind, not in the XAML):
Observable.Interval(TimeSpan.FromSeconds(1)).Take(10).ForEachAsync(x =>
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
new MessageDialog("seconds: " + (10-x), "timer").ShowAsync();
});
});
The big plus of this approach - your code will more portable between frameworks. Hope this helps.
来源:https://stackoverflow.com/questions/10455275/countdown-timer-for-metro-app