Silverlight Timer

在上海的时候问过SL开发团队Timer的使用问题,他们给了一个很好用的timer,就是下文中介绍的,Google了一下找到这篇文章,跟大家分享。

定时器(Timer)是一个挺常见的功能。通过使用定时器,  可以每隔一段制定的时间后触发某一指定的事件,如刷新、定时提醒等等。那在Silverlight中怎么使用定时器呢?其实也很简单,且听我细细道来。:)

HtmlTimer类和Storyboard

   HtmlTimer timer = new HtmlTimer();

        timer.Interval = 200; //200毫秒

        timer.Tick += new EventHandler(timer_Tick);

        timer.Start();

        void timer_Tick(object sender, EventArgs e)

        {

            //在这里处理定时器事件

        }

 <Canvas.Resources>

    <Storyboardx:Name="timer"Completed="timer_Tick" />

       
        timer.Duration = new TimeSpan(0, 0, 0, 0, 200); //200毫秒

        timer.Begin();

        void timer_Tick(object sender, EventArgs e)

        {

            //在这里处理定时器事件

            timer.Begin();

 
 

定时器控件

    public class XamlControl : Control

    {

        private readonly FrameworkElement m_Container;

        protected FrameworkElement Container

        {

            get { return m_Container; }

        }

        protected XamlControl(string xamlName)

        {

            Stream stream = GetType().Assembly.GetManifestResourceStream(xamlName);

            if (stream == null)

            {

                throw new ArgumentException("Xaml resource " + xamlName + " not present", "xamlName");

            }

            using (StreamReader sr = new StreamReader(stream))

            {

                string xamlData = sr.ReadToEnd();

                m_Container = base.InitializeFromXaml(xamlData);

            }

        }

 
 

<Canvas

 xmlns="http://schemas.microsoft.com/client/2007"

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

 >

 <Canvas.Triggers>

    <EventTriggerRoutedEvent="Canvas.Loaded">

      <EventTrigger.Actions>

        <BeginStoryboard>

          <Storyboardx:Name='timer'>

          </Storyboard>

        </BeginStoryboard>

      </EventTrigger.Actions>

    </EventTrigger>

 </Canvas.Triggers>

</Canvas>

 
 

    public class Timer : XamlControl

    {

        private Storyboard m_Timer;

        public Timer()

            : base("SilverlightControl.Timer.xaml")

        {

            if (Container == null) return;

            m_Timer = Container.FindName("timer") as Storyboard;

            m_Timer.Completed += OnComplete;

        }

        public TimeSpan Interval

        {

            get

            {

                return m_Timer.Duration.TimeSpan;

            }

            set

            {

                m_Timer.Duration = new Duration(value);

            }

        }

        public event EventHandler<EventArgs> Tick;

        private void FireTick()

        {

            Tick(this, new EventArgs());

        }

        private void OnComplete(object sender, EventArgs e)

        {

            FireTick();

            m_Timer.Begin();

        }

 
 
 
 

                Timer timer = new Timer();

                timer.Interval = new TimeSpan(0, 0, 0, 0, 200);

                timer.Tick += timer_tick;

 
 
 
 
 
 
 
关于Timer的信息,还可以参考下文:
 
原文链接: 在Silverlight中使用定时器(Timer) 

转载于:https://www.cnblogs.com/BryanChow/archive/2007/09/28/909613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值