WPF编程里让TextBlock动态的显示时间
展开全部
Xaml如下:
<Window x:Class="NetExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" x:Name="txt_timeShow" Text="00:00:00" Width="200" Height="30" TextAlignment="Center" FontSize="24"/>
<Button Grid.Row="2" x:Name="btn_StartStop" Content="Start" Width="80" Height="30" HorizontalAlignment="Center"
Click="btn_StartStop_Click_1"
/>
</Grid>
</Window>
CS代码部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 NetExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitialTimer();
}
private DispatcherTimer _Timer;
private int _TimeDelay = 0;
private int count = 0;
void InitialTimer(){
_Timer = new DispatcherTimer();
_TimeDelay = 1000;
_Timer.Interval = TimeSpan.FromMilliseconds(_TimeDelay);
_Timer.Tick += _Timer_Tick;
}
private void _Timer_Tick(object sender, object e)
{
//处理
ShowTimeString(++count);
}
private string FormatTimeString(int value)
{
return value < 10 ? "0" + value.ToString() : value.ToString();
}
private void ShowTimeString(int count)
{
int hour = count / 3600;
int minute = (count % 3600) / 60;
int second = (count % 3600) % 60;
txt_timeShow.Text = FormatTimeString(hour) + ":" + FormatTimeString(minute) + ":" + FormatTimeString(second);
}
private void btn_StartStop_Click_1(object sender, RoutedEventArgs e)
{
if (_Timer.IsEnabled)
{
btn_StartStop.Content = "Start";
_Timer.Stop();
}
else
{
btn_StartStop.Content = "Stop";
_Timer.Start();
}
}
}
}
你自己可以增加重新计时,只要将count清零就行
<Window x:Class="NetExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" x:Name="txt_timeShow" Text="00:00:00" Width="200" Height="30" TextAlignment="Center" FontSize="24"/>
<Button Grid.Row="2" x:Name="btn_StartStop" Content="Start" Width="80" Height="30" HorizontalAlignment="Center"
Click="btn_StartStop_Click_1"
/>
</Grid>
</Window>
CS代码部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 NetExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitialTimer();
}
private DispatcherTimer _Timer;
private int _TimeDelay = 0;
private int count = 0;
void InitialTimer(){
_Timer = new DispatcherTimer();
_TimeDelay = 1000;
_Timer.Interval = TimeSpan.FromMilliseconds(_TimeDelay);
_Timer.Tick += _Timer_Tick;
}
private void _Timer_Tick(object sender, object e)
{
//处理
ShowTimeString(++count);
}
private string FormatTimeString(int value)
{
return value < 10 ? "0" + value.ToString() : value.ToString();
}
private void ShowTimeString(int count)
{
int hour = count / 3600;
int minute = (count % 3600) / 60;
int second = (count % 3600) % 60;
txt_timeShow.Text = FormatTimeString(hour) + ":" + FormatTimeString(minute) + ":" + FormatTimeString(second);
}
private void btn_StartStop_Click_1(object sender, RoutedEventArgs e)
{
if (_Timer.IsEnabled)
{
btn_StartStop.Content = "Start";
_Timer.Stop();
}
else
{
btn_StartStop.Content = "Stop";
_Timer.Start();
}
}
}
}
你自己可以增加重新计时,只要将count清零就行
追问
我想显示毫秒,把_TimeDelay的值改为了1,但运行的结果和标准的时间差距挺大。应该怎么改呢?
追答
你设置千分之一秒刷新一次,希望一毫秒一毫秒跳动,UI线程都来不及处理。不同计算机的性能之间存在的差异也影响时间刷新,我们这种普通的计算机1秒钟跳动1000次根本不可能。你设置_TimeDelay为1,系统在执行时,也只会按照实际可分配的时间片执行,但时间变化是正确的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询