xaml code

--------------------------------------------------------------------------------------


    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300">

   

       

           

               

               

               

               

               

               

               

           


           

               

               

           


           

           

           

           

           

           

           

           

       


       

           

       


   



-------------------------------------------------------------------------------

cs code

-------------------------------------------------------------------------------



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Threading;

namespace TimerExample

{

    ///



    /// Interaction logic for Window1.xaml

    ///


    public partial class Window1 : Window

    {

        private Clock clk; //This is a class which keeps track of the time being displayed

        private System.Windows.Threading.DispatcherTimer TimerClock;

      

        int changepic = 0;

       

        public Window1()

        {

            InitializeComponent();            

        }

        

        private void OnClick(object sender, RoutedEventArgs e)

        {           

            //if (ValidateValues(hr.Text, min.Text, sec.Text))

            //{

            status.Text = "starting...";

                TimerClock = new System.Windows.Threading.DispatcherTimer();

                //Creates a timerClock and enables it

                TimerClock.Interval = new TimeSpan(0, 0, 1);

                TimerClock.IsEnabled = true;

                clk = new Clock(hr.Text, min.Text, sec.Text);

                TimerClock.Tick += new EventHandler(TimerClock_Tick);                

            //}

        }

        private void OnClick1(object sender, RoutedEventArgs e)

        {           

            TimerClock.IsEnabled = false;

            Clock.hr = Clock.min = Clock.sec = 0;

            hr.Text = Clock.hr.ToString();

            min.Text = Clock.min.ToString();

            sec.Text = Clock.sec.ToString();            

        }

       

        void TimerClock_Tick(object sender, EventArgs e)

        {           

            clk.Decrement();

          

            hr.Text = Clock.hr.ToString();

            min.Text = Clock.min.ToString();

            sec.Text = Clock.sec.ToString();

           

            //change pic

            changepic = Clock.sec % 6+1;

            string text = "cat0" + changepic.ToString()+".gif";

            //myImage.Source = "cat02.gif";

            myImage.Source = new BitmapImage(new Uri(text, UriKind.Relative));

            if (Clock.completed)

            {               

                TimerClock.IsEnabled = false;

                //SoundPlayer player = new SoundPlayer(@"gurgle.wav");

                //Plays the spoilsport of your sleep ;)               

                //player.Play();

            }       

        }

       

        private bool ValidateValues(string hr1, string min1, string sec1)

        {              

            //Validate values

            return true;

        }        

        

    }

        public partial class Clock

        {       

            public static int hr, min, sec;

            public static bool completed;

            public Clock() { }

            public Clock(string hr1, string min1, string sec1)

            {           

                completed = false;

                hr = int.Parse(hr1);

                min = int.Parse(min1);

                sec = int.Parse(sec1);

            }

            public void Decrement()

            {

                //Decrement the hours/mins/secs       

                sec++;

                if (sec >= 60)

                    min =(int)Math.Floor((decimal)sec / 60);

                if (sec >= 3600)

                    hr = (int)Math.Floor((decimal)sec / 3600);                

                

            }

            public string TimeLeft()

            {           

                return (hr.ToString() + ":" + min.ToString() + ":" + sec.ToString());

            }

        }  

}

---------------------------------------------------------------------------------------


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 狼翔月影 的頭像
    狼翔月影

    狼翔天地

    狼翔月影 發表在 痞客邦 留言(0) 人氣()