【发布时间】:2011-11-02 02:16:15
【问题描述】:
我有一个 For..Each 循环,它遍历所有网格的矩形并随机更改它们的填充。我用彩色动画改变填充。这是我的代码:
Dim rand as new random
Dim changeColor As New Animation.ColorAnimation
changeColor.Duration = TimeSpan.FromSeconds(0.5)
For Each r As Rectangle In Grid.Children
changeColor.From = ColorConverter.ConvertFromString(r.Fill.ToString)
Dim i As Integer = rand.Next(0, 2)
Select Case i
Case 0
changeColor.To = Colors.White
Case 1
changeColor.To = Colors.Gray
End Select
Animation.Storyboard.SetTarget(changeColor, r)
Animation.Storyboard.SetTargetProperty(changeColor, New PropertyPath("Fill.Color"))
Dim sb As New Animation.Storyboard
sb.Children.Add(changeColor)
sb.Begin()
System.Threading.Thread.Sleep(0.5)
Next
问题是循环不休眠。我想触发动画,然后等到矩形填充改变,然后继续其余的,但似乎所有的矩形填充都同时改变了。那我做错了什么?
【问题讨论】:
-
看起来你正在让主线程进入睡眠状态。这意味着动画可能在 thread.sleep 完成之前甚至不会开始。