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 ="tbkTimer" Width="300"="50" FontSize="30" Foreground="Red"/> </GridUserControl>
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,1);
创建事件处理
timer.Tick += new EventHandler(timer_Tick);
开始计时
timer.Start();
}
void timer_Tick(object sender,EventArgs e)
{
输出时间
tbkTimer.Text = "当前时间:" + DateTime.Now.ToLongTimeString();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。