因为WPF中没有timer控件,所以实际上是使用DISPATCHERTIMER创建定时操作。代码如下
Class MainWindow
Private WithEvents JTimer As New Threading.DispatcherTimer With {.Interval = TimeSpan.FromSeconds(0.5)} '至少设置一个属性值用来初始化}
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
JTimer.Interval = TimeSpan.FromSeconds(2) '转换时间格式 2 就是2秒
JTimer.Start() '开始计数器
End Sub
Private Sub JTimer_Tick(sender As Object, e As EventArgs) Handles JTimer.Tick
Me.Title = (Now)
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
JTimer.Stop() '停止timer计数
End Sub
End Class
代码源于WPF VB语言使用DISPATCHERTIMER创建定时操作_happyrighthand的博客-CSDN博客,我在代码注释中加入了自己的理解。
另外一种用timers.timer创建定时操作的方法见这篇文章 vb.net在WPF中创建timer的另一种方法