namespace PizzaSoftware.UI
{
/// <summary>
/// Interaction logic for LoginForm.xaml
/// </summary>
public partial class LoginForm : Window
{
public LoginForm()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Timer timer = new Timer(1000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
lblCurrentTime.Content = DateTime.Now.ToShortTimeString();
}
}
}
基本上,我只是想在我的表单上有一个显示当前时间的标签。我正在使用另一个我的 SO 问题中建议的计时器。
我收到标题错误。我能做些什么来解决这个问题?
请您参考如下方法:
嗯,最简单的方法是使用 DispatcherTimer - 这将触发它的Tick调度程序线程中的事件:
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(1000);
timer.IsEnabled = true;
timer.Tick += timer_Elapsed;
或者,您可以使用
Dispatcher.Invoke/
BeginInvoke在调度程序线程中执行委托(delegate)......但这会更复杂。如果您需要在每个计时器滴答声上做大量工作,然后只用结果更新 UI,这将是一种更好的方法 - 但是由于您在这里根本没有做太多工作,所以只需在 UI 中完成所有工作就可以了线。




