游戏计时器
<Window x:Class="WpfTimer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="482.836" Width="630.97"> <Canvas Margin="0,0,0,0"> <TextBlock Canvas.Left="211" Height="100" Name="time_TextBlock" Text="" Width="194" FontSize="22" Foreground="#FFFCFC00" HorizontalAlignment="Center" TextAlignment="Center" /> <Button Canvas.Left="10" Canvas.Top="70" Content="开始游戏" Height="30" Name="startStopButton" Width="100" Click="StartOrStop" /> <Button Canvas.Left="10" Canvas.Top="10" Content="重置" Height="30" Name="resetButton" Width="100" Click="Reset" /> <TextBlock Canvas.Left="10" Height="251" Name="timeRecord_TextBlock" Width="508" FontSize="20" Foreground="#FFFCFC00" HorizontalAlignment="Center" TextAlignment="Center" Canvas.Top="178" /> </Canvas> </Window> <Window x:Class="WpfTimer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="482.836" Width="630.97"> <Canvas Margin="0,0,0,0"> <TextBlock Canvas.Left="211" Height="100" Name="time_TextBlock" Text="" Width="194" FontSize="22" Foreground="#FFFCFC00" HorizontalAlignment="Center" TextAlignment="Center" /> <Button Canvas.Left="10" Canvas.Top="70" Content="开始游戏" Height="30" Name="startStopButton" Width="100" Click="StartOrStop" /> <Button Canvas.Left="10" Canvas.Top="10" Content="重置" Height="30" Name="resetButton" Width="100" Click="Reset" /> <TextBlock Canvas.Left="10" Height="251" Name="timeRecord_TextBlock" Width="508" FontSize="20" Foreground="#FFFCFC00" HorizontalAlignment="Center" TextAlignment="Center" Canvas.Top="178" /> </Canvas> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace WpfTimer { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public delegate void TimerDelegate(); private bool calculate = false; //开始按钮状态 private double num = 0; // 前台显示的计时 private DateTime startTime; // 计时开始时间 private static Timer timer; public MainWindow() { InitializeComponent(); timeRecord_TextBlock.Text = "玩家记录"; } //开始,总是从0开始计时 private void StartOrStop(object sender, EventArgs e) { if (calculate) { calculate = false;// startStopButton.IsEnabled = false; startStopButton.Content = "开始游戏"; timeRecord_TextBlock.Text += " " + time_TextBlock.Text; resetButton.IsEnabled = true; } else { calculate = true; startTime = DateTime.Now; timer = new Timer(TimeMethod, null, 100, 100); startStopButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new TimerDelegate(TimerCal)); startStopButton.Content = "确认完成"; resetButton.IsEnabled = false; } } private void TimerCal() { time_TextBlock.Text = num.ToString(); if (calculate) { startStopButton.Dispatcher.BeginInvoke( System.Windows.Threading.DispatcherPriority.Background, new TimerDelegate(this.TimerCal)); } } private void TimeMethod(object state) { DateTime currentTime = DateTime.Now; num = (currentTime - startTime).TotalSeconds; } private void Reset(object sender, RoutedEventArgs e) { num = 0; time_TextBlock.Text = num.ToString(); timer.Dispose(); startStopButton.IsEnabled = true; timeRecord_TextBlock.Text = ""; } } }
---------------------------------------------------线程计时延时执行------------------------------------------------------------------------------------
WPF DISPATCHERTIMER(定时器应用) 如果无人触摸:60s自动关闭窗口 xmal:部分 <s:SurfaceWindow x:Class="SurfaceApplication1.SurfaceWindow1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="http://schemas.microsoft.com/surface/2008" Title="SurfaceApplication1" TouchDown="SurfaceWindow_TouchDown" > <Grid > <Button Width="80" Height="80" Background="Yellow" Click="Button_Click">OK</Button> <Label x:Name="lblSeconds"> 你好!</Label> </Grid> </s:SurfaceWindow> cs:部分 //60s无人操作自动关闭 DispatcherTimer dTimer; private void Button_Click(object sender, RoutedEventArgs e) { //构造一个DispatcherTimer类实例 dTimer = new System.Windows.Threading.DispatcherTimer(); //设置事件处理函数 dTimer.Tick += new EventHandler(dispatcherTimer_Tick); } private void dispatcherTimer_Tick(object sender, EventArgs e) { this.Close(); } //触摸后重新给“i”赋值 private void SurfaceWindow_TouchDown(object sender, TouchEventArgs e) { int i = 60; //定时器时间间隔1s if ( dTimer.Interval!=null) { dTimer.Interval = new TimeSpan(0, 0, i); dTimer.Start(); } }
using System.Windows.Threading;
/// <summary> /// 计时器 /// </summary> DispatcherTimer dTimer; private void test() { dTimer = new System.Windows.Threading.DispatcherTimer(); dTimer.Interval = new TimeSpan(0,0,2); //设置事件处理函数 dTimer.Tick += new EventHandler(dispatcherTimer_Tick); dTimer.Start(); } private void dispatcherTimer_Tick(object sender,EventArgs e) { this.Close(); }