代码
public partial class MainPage : UserControl
{
int i = 1;
TextBlock myTextBlock;
public MainPage()
{
InitializeComponent();
myTextBlock = new TextBlock();
myTextBlock.Loaded += this.StartTimer;
myTextBlock.Margin = new Thickness(30);
LayoutRoot.Children.Add(myTextBlock);
}
public void StartTimer(object o, RoutedEventArgs sender)
{
//创建计时器
System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
//创建间隔时间
myDispatcherTimer.Interval = new TimeSpan(0,0,0,1);
//创建到达间隔时间后需执行的函数
myDispatcherTimer.Tick += new EventHandler(Each_Tick);
//开启计时器
myDispatcherTimer.Start();
}
public void Each_Tick(object o, EventArgs sender)
{
myTextBlock.Text = i++.ToString() + " 秒";
}
}