• Twitter
03
November
Comments

I have gotten a few requests and questions regarding using a Timer Control in C# for WP7(.5) as many are shocked a slap-on control doesn’t exist like VB or it would be much simpler but if you were speaking to me, I would say stop talking for a second and really tell you it’s much simpler than the MSDN (Microsoft Code Page, yeah, can be confusing) page is letting off and with that being said, let’s get on with it my lovely (Creepy right? Well, pretend this was for my Halloween Post?).

1). Find your MainPage.xaml.cs (or whatever form and it’s .xaml.cs) and look for:

namespace PhoneAppName
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{

2). Now that we found it, let’s make it look a little like this… Kidding, make it COMPLETELY like this, unless you dislike my comments, or code… :(

namespace PhoneAppName

{

public partial class MainPage : PhoneApplicationPage

{

public MainPage()

{

InitializeComponent(); // Required; Unsure why? Stop programming.

System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
//Note: Normally you’ll see coding just showing you DispatcherTimer and probably get an error, so I went one step ahead and gave you a fool proof way of using it.

double val = 5; // To prevent double errors, we’re predefining to PREVENT! So, I am using 5 as in 5 seconds (not the best way to do it but works!).

System.TimeSpan time = TimeSpan.FromSeconds(val); // We’re telling it to use Seconds, you can use even Milliseconds if you need to?

timer.Interval = time; // Now set the timer to “Tick” based on the interval we pre-determined.
// If it was a game, you could set buttons to have specific times based on how long a user wants to play or if a user gets xx points extend time, & etc…

timer.Tick += new EventHandler(timer_Tick); // Now we’re saying, hey, if you’re going to Tick, you’re going to WORK but where? Follow me (see below).

timer.Start(); // Oh yeah, you know you have to turn the darn thing on, right? Yeah, it is also the same method to stop (hint: start is opposite of…).
}

3). Well, let’s see what makes it Tick, haha! Get it? Tick, like what makes it work! Ok, moving on… Write/Copy below:

private void timer_Tick(object sender, EventArgs e)
{
PageTitle.Text = DateTime.Now.TimeOfDay.TotalSeconds.ToString(); // I used PageTitle only because I don’t want to confuse you and make you do any work…
}

4). Run it baby, run it! If you’re a master Copy/Paster, you should have done it wonderfully and wait… It’s not showing the seconds?! Ha, I am tricky in my young and soon to be old age (Nov 4, 2011 will be 26; Yes tomorrow (assuming you’re reading this Nov 3rd, 2011). ┬áSurely you didn’t think you’d have me do all the work did you?

Come on, you must know to parse the data you’d use some form of time stamp like “hh:mm:ss tt” and that basically means you want the 12-hour format with hours, minutes, seconds and time of day (AM/PM). Wait, what, you want 24-Hour Military time? Yes drill sar… Eh, just change the lower-cased “hh” to “HH” as it is manly, and ‘Militaryish’ and ready to go in to War!

All jokes aside, I do hope you understand now how to implement a timer control for your game or whatever purpose you have, maybe even for a clock but this should help you along the way… I do hope you become a subscriber, use my RSS, follow me on Twitter (@LanceSeidman) or donate some cash but if you don’t want to donate to me, share me to all the ladies and by ladies… You know…

Leave a Reply

You must be logged in to post a comment.

Sponsored by


PLEASE DISABLE YOUR POPUP STOPPER!

My Windows Phone 7/8 App's (Many Not Updated)

waitlist (Free) for WP8 Health Center (Free) for WP8 Speechy (Free) for WP7 TWiT (Free) for WP7 Space Weight (Free) for WP7 I.T. Guy (Free) for WP7 Job Discovery (Free) for WP7 Spelling Bee (Free) for WP7 Coin Toss Up (Free) for WP7 BIG Dialer (Free) for WP7



MY Windows 8 App's (Many Not Updated)

MyTube (Free) for Windows 8/RT

  • Member of The Internet Defense League