silverlight中的多线程和计时器

来源:互联网 发布:淘宝价格区间怎么设置 编辑:程序博客网 时间:2024/06/05 02:08

DispatcherTimer 是SL中很有用的一个计时器对象。也是基础SL 的UI线程

XAML代码:

复制代码
<UserControl x:Class="Sample.Timer"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Width="400" Height="200"><Grid x:Name="LayoutRoot" Background="White">    <!--背景-->    <Rectangle Fill="Gold" Stroke="Black"                StrokeThickness="3"                RadiusX="5" RadiusY="5"/>    <!--显示时间-->    <TextBlock x:Name="tbkTimer"                Width="300" Height="50"                FontSize="30" Foreground="Red"/></Grid></UserControl>
复制代码

 

后台代码:

复制代码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Windows.Threading;namespace Sample{    public partial class Timer : UserControl    {public Timer(){    InitializeComponent();    //创建DispatcherTimer    DispatcherTimer timer = new DispatcherTimer();    //设置间隔1秒    timer.Interval = new TimeSpan(0, 0, 1);       //创建事件处理    timer.Tick += new EventHandler(timer_Tick);    //开始计时    timer.Start();}void timer_Tick(object sender, EventArgs e){    //输出时间    tbkTimer.Text = "当前时间:" + DateTime.Now.ToLongTimeString();}    }}
复制代码
原创粉丝点击