我为快速简单的UI动画制作了这个功能
Delegate Sub AnimateDoubleSetter(value As Double)
Sub AnimateDouble(Setter As AnimateDoubleSetter, [From] As Double, [To] As Double, DurationMs As Double)
Dim t As New System.Windows.Threading.DispatcherTimer With {.Interval = New TimeSpan(400000)}
Dim StartTime = DateTime.Now
Dim Diff = [To] - [From]
AddHandler t.Tick, Sub()
Dim ms = (DateTime.Now - StartTime).TotalMilliseconds
Dim fraction = Math.Min(ms / DurationMs, 1)
If fraction = 1 Then t.Stop()
Setter([From] + Diff * fraction)
End Sub
t.Start()
End Sub
我像这样使用它
AnimateDouble(Sub(x As Double) cBackground.Opacity = x, 0, 0.3, 200)
虽然我想到,因为cBackground.Opacity是一个属性,它已经是一对由getter / setter组成的函数。是否有可能从属性中提取setter函数,以便我可以传递它甚至属性本身?