Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
Search :       Advanced Search �
Home

Author Rank :
Page Views :
Downloads : 0
Rating :
 Rate it
Level :
Become a Sponsor
Tags


Here you'll see a very breif but straight to the point tutorial on the actual breakdown of the Timer control that you need to make in C#.NET for your Windows Phone 7 project.

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 quite simple, so let's get on with it!

  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()

        {

            InitializeComponent();
    // Required

            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.
        }
    }
    }
  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.
  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 works¦

    }


  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?! 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 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.

Comment Request!

Thank you for reading this post. Please post your feedback, question, or comments about this post Here.

 [Top] Rate this article
 
 About the author
 
Author
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
 Comments

 � 2024  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.