首 页IT知识库翔宇问吧收藏内容
当前位置:翔宇亭IT乐园IT知识库WEBRIA

在Silverlight中使用定时器(Timer)

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2011-02-12 07:16:52

本文通过一个实例讲解了在Silverlight中使用定时器(Timer)的功能。Timer定时器经常用于每隔一段特定的时间后触发某一指定的事件,如刷新、定时提醒等等。

在Silverlight中使用到两个类:HtmlTimer类或Storyboard来实现定时器(Timer)。我们先说说使用HtmlTimer类创建定时器(Timer)的方法。HtmlTimer位于System.Windows.Browser命名空间中。其定时器(Timer)的定义源代码如下:

HtmlTimer timer = new HtmlTimer();
timer.Interval = 200; //间隔200毫秒
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
void timer_Tick(object sender, EventArgs e)
{
     //在这里处理定时器事件
}

这个类被标记为obsolete,编译时会报一个warning,因为使用这个类定义的定时器精度不高,对于时间间隔短的动画不是很好,在今后的正式版本可能会移除对这个类的支持。为了保证现在代码能够平稳的过渡到以后的正式版本,我们可以使用Storyboard来实现定时器的功能。

首先在Page.xaml文件中添加一个Storyboard的资源:

<Canvas.Resources>
    <Storyboard x:Name="timer" Completed="timer_Tick" />
</Canvas.Resources>

然后在代码中设置定时器的间隔,并开启动画,然后在Completed中重新开始Storyboard就可以模拟定时器的行为了:

timer.Duration = new TimeSpan(0, 0, 0, 0, 200); //200毫秒
timer.Begin();
void timer_Tick(object sender, EventArgs e)
{
            //在这里处理定时器事件
            timer.Begin();
 }

定时器控件

下面把它包装成一个控件,这样以后我们就可以很方便的在程序中使用定时器了。为了方便从Xaml文件创建控件,我们先定义一个基类

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);
         }
    }
}

然后我们在工程中添加一个xaml文件,在Properties面板-Advanced-Build Action中选择”Embedded Resource”,这样我们就可以在代码里动态的加载这个xaml文件了。文件的内容如下

<Canvas
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas.Triggers>
    <EventTrigger RoutedEvent="Canvas.Loaded">
      <EventTrigger.Actions>
        <BeginStoryboard>
          <Storyboard x:Name='timer'>
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger.Actions>
    </EventTrigger>
  </Canvas.Triggers>
</Canvas>


下面是定时器控件的代码(需要将下面的SilverlightControl改成你自己建立的工程的名称):

    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();
     }
}

这样我们就可以用类似于HtmlTimer的语法,很方便的操作定时器控件了 

Timer timer = new Timer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 200);
timer.Tick += timer_tick;

Children.Add(timer); //开始计时

如果要停止计时,只需要用Children.Remove(timer)即可一点需要注意的地方,当你调用Storyboard.Begin的时候,如果这个Storyboard还没有被加入Xaml的DOM树中,如下图中右下角的红色节点所示,那么这时Silverlight会抛出异常。这在你将Storyboard内嵌在自定义控件里的时候要特别小心,如果你的控件不在当前Xaml的DOM树中,而你调用了这个控件内包含的Storyboard的Begin方法或者Stop方法,都会产生异常。

本文源地址:http://blog.csdn.net/SilverlightShanghai/archive/2007/09/02/1768874.aspx

本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!

微信搜索“优雅的代码”关注本站的公众号,或直接使用微信扫描下面二维码关注本站公众号,以获取最新内容。

个人成长离不开各位的关注,你的关注就是我继续前行的动力。

知识评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
愿您的精彩评论引起共鸣,带来思考和价值。
用户名: 查看更多评论
分 值:100分 90分 80分 70分 60分 40分 20分
内 容:
验证码:
关于本站 | 网站帮助 | 广告合作 | 网站声明 | 友情连接 | 网站地图
本站部分内容来自互联网,如有侵权,请来信告之,谢谢!
Copyright © 2007-2023 biye5u.com. All Rights Reserved.