WP7 – A simple clock example for Windows Phone in 10-15 minutes

By -






I have started to learn WP7 through reading e-book, here i would like to share the Simple clock example which i have practiced just now. Its very easy to understand and
implement.


MainPage.xaml

 
            
        


Below is the entire code file. And yes include System.Windows.Threading namespace because DispatcherTimer resides in it and its not included by default. The constructor initializes the DispatcherTimer, instructing it to call OnTimerTick once every second. The event handler onTimerTick simply converts the current time to a string to set it to the TextBlock.

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Threading;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace _1OrientationDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            DispatcherTimer timer  = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += OnTimerTick;
            timer.Start();
        }

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

    }
}
Paresh Mayani

Paresh Mayani is a lead android developer from India. Currently, He is Sr. Software engineer at InfoStretch Solutions Pvt. Ltd. He has been developing android apps since very 1st version i.e. Android 1.5, he says he has seen days and nights of Android history. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad