在Silverlight中实现定时器功能

Posted on August 29, 2007 by Fdream

上次写了一篇WPF中的定时器类,那个定时器类在Silverlight中也被精简下岗了(还有许多常用的方法或被精简了或直接下岗了,使用起来远没有WPF方便……),定时器还得用,而Timer类实在是不好用(都是因为线程……),只有另想办法了。有个不错的方式,通过Storyboard来实现,把Storyboard的时间设置为一秒,然后在这个Storyboard结束后进行你想要的操作,同时重新开始这个Storyboard,那么这就相当于一个一秒的定时器了,方法很巧。实现代码如下:

loopTimer = new Storyboard();
loopTimer.Duration = TimeSpan.FromSeconds(1);
loopTimer.SetValue(Storyboard.NameProperty, “looptimer”);
this.Resources.Add(loopTimer);
loopTimer.Completed += new EventHandler(loopTimer_Completed);
loopTimer.Begin();

void loopTimer_Completed(object sender, EventArgs e)
{
loopTimer.Begin();
//在这里做你想做的事吧~
}

分享 |
Categories:
Silverlight/C#
Tags:
Comments:
4 Comments
Views:
13,085 Views

Related Posts

4 Responses to <在Silverlight中实现定时器功能>

  1. 2 says:

    [eek][cry][cool][cool][cool][cool][cool][cool][cool][cool][cool][cool][cool][cool][cool][cool][rolleyes][redface][wink][twisted][razz][question][mrgreen][neutral][frown][frown][question][confused][mrgreen][neutral]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>