我正在尝试用VB.Net编写一台迪斯科灯光机。 我在WPF上有四个椭圆,我希望它们“点亮”(=将填充从白色更改为某种颜色),然后等待 0.5秒,然后将填充更改回白色 - 一切都按照预先写好的顺序进行。
我正在尝试使用DispatherTimer但我实际上并不知道如何使其工作。 省略号是名称pad0,pad1等......
Public Sub timer()
Dim t As New System.Windows.Threading.DispatcherTimer()
AddHandler t.Tick, AddressOf dispatcherTimer_Tick
t.Interval = New TimeSpan(0, 0, 1)
End Sub
Private Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub play_Click(sender As Object, e As RoutedEventArgs) Handles play.Click
Dim sequence = New Integer() {1, 0, 3, 2}
For i As Integer = 0 To 3
Select Case sequence(i)
Case 0
pad0.Fill = Brushes.Blue
**this is where I want the timer to run!**
padOff(pad0)
Case 1
pad1.Fill = Brushes.Yellow
**this is where I want the timer to run!**
padOff(pad1)
Case 2
pad2.Fill = Brushes.Green
**this is where I want the timer to run!**
padOff(pad2)
Case 3
pad3.Fill = Brushes.Red
**this is where I want the timer to run!**
padOff(pad3)
End Select
Next
End Sub
Public Sub padOff(ByVal pad As Shape)
pad.Fill = Brushes.White
End Sub
答案 0 :(得分:4)
用户界面仅在所有代码执行完毕后才会更新。因此,您没有看到对Blue的更改。更糟糕的是,你的用户界面完全冻结了0.5秒。
执行此操作的正确方法是:
其他替代方案包括:
await Task.Delay(500)
),这会导致在等待期间更新和响应用户界面。