'timer'에 해당되는 글 1건

  1. 2008.12.17 Silverlight Timer

Silverlight 에서 타이머를 만들려면 System.Windows.Threading 네임스페이스의 DispatcherTimer 클래스를 사용합니다.


1초마다 시간을 업데이트하는 예제.

Xaml
<TextBlock x:Name="tbTime" />

CS

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace SLTimer
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            this.LayoutRoot.Loaded += new RoutedEventHandler(LayoutRoot_Loaded);
        }

        void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            tbTime.Text = DateTime.Now.ToString();
        }
    }
}



Posted by glycerine
,