我想创建 wpf 倒数计时器,将结果显示为hh:mm:ss进入文本框,我将感谢任何人的帮助。
您可以使用DispatcherTimer
class (msdn http://msdn.microsoft.com/en-US/library/system.windows.threading.dispatchertimer.aspx).
您可以坚持的时间长度TimeSpan
结构 (msdn http://msdn.microsoft.com/en-us/library/system.timespan.aspx).
如果你想格式化TimeSpan
to hh:mm:ss
你应该调用ToString
带有“c”参数的方法(msdn http://msdn.microsoft.com/en-us/library/dd992632.aspx).
Example:
XAML:
<Window x:Class="CountdownTimer.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>
<TextBlock Name="tbTime" />
</Grid>
</Window>
隐藏代码:
using System;
using System.Windows;
using System.Windows.Threading;
namespace CountdownTimer
{
public partial class MainWindow : Window
{
DispatcherTimer _timer;
TimeSpan _time;
public MainWindow()
{
InitializeComponent();
_time = TimeSpan.FromSeconds(10);
_timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
{
tbTime.Text = _time.ToString("c");
if (_time == TimeSpan.Zero) _timer.Stop();
_time = _time.Add(TimeSpan.FromSeconds(-1));
}, Application.Current.Dispatcher);
_timer.Start();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)