看起来如果您只是从默认为Days
. 如果您使用Double.TryParse
,则可以使用该TimeSpan.FromSeconds
方法来创建您的持续时间。看看这是否适合你。
Public Sub Set_Laptime(ByVal sender, ByVal KeyUpEventArgs, ByVal e)
Dim seconds As Double
Dim ts As TimeSpan
Dim LapTime As Duration
If Double.TryParse(TextBox1.Text, seconds) Then
ts = TimeSpan.FromSeconds(seconds)
LapTime = New Duration(ts)
Else
MessageBox.Show("Invalid Entry -Please Try Again")
TextBox1.Text = "0"
End If
e.Handled = True
End Sub
添加了代码以使用早期问题中的 OP 代码显示 Dependecy 属性。
首先将 Xaml 中的 DoubleAnimations 更改为:
<Storyboard x:Key="MyPathAnimation">
<DoubleAnimationUsingPath Storyboard.TargetName="Ellipse1"
Storyboard.TargetProperty="(Canvas.Left)"
PathGeometry="{DynamicResource Daytona}"
Duration="{Binding Path=getLapTime}" RepeatBehavior="Forever" Source="X" FillBehavior="Stop" />
<DoubleAnimationUsingPath Storyboard.TargetName="Ellipse1"
Storyboard.TargetProperty="(Canvas.Top)"
PathGeometry="{StaticResource Daytona}"
Duration="{Binding Path=getLapTime}" RepeatBehavior="Forever" Source="Y" FillBehavior="Stop" />
</Storyboard>
然后将您的用户控件中的代码更改为:
Imports System.Windows.Media.Animation
Public Class UserControl1
Public Sub runPathAnimation()
Dim sb As Storyboard = CType(FindResource("MyPathAnimation"), Storyboard)
sb.Begin()
End Sub
Shared myDependencyProperty As DependencyProperty = DependencyProperty.Register("getLapTime", GetType(Duration), GetType(UserControl1))
Public Property getLapTime As Duration
Get
Return CType(GetValue(myDependencyProperty), Duration)
End Get
Set(value As Duration)
SetValue(myDependencyProperty, value)
End Set
End Property
Private Sub set_Laptime_KeyUp(sender As System.Object, e As System.Windows.Input.KeyEventArgs)
Dim seconds As Double
Dim ts As TimeSpan
If Double.TryParse(set_Laptime.Text, seconds) Then
ts = TimeSpan.FromSeconds(seconds)
getLapTime = New Duration(ts)
Else
MessageBox.Show("Invalid Entry -Please Try Again")
set_Laptime.Text = "0"
End If
e.Handled = True
End Sub
'Alternate input method to allow inputting hours, minutes and seconds
Private Sub set_Laptime_KeyUp(sender As System.Object, e As System.Windows.Input.KeyEventArgs)
If e.Key = Key.Enter Then
Dim seconds As Integer
Dim hours As Integer
Dim minutes As Integer
Dim split As String() = New String() {":"}
Dim input As String() = set_Laptime.Text.Split(split, StringSplitOptions.None)
Dim ts As TimeSpan
If input.Length > 0 Then Integer.TryParse(input(0), hours)
If input.Length > 1 Then Integer.TryParse(input(1), minutes)
If input.Length > 2 Then Integer.TryParse(input(2), seconds)
ts = New TimeSpan(hours, minutes, seconds)
getLapTime = New Duration(ts)
e.Handled = True
End If
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.DataContext = Me 'Very important will not work if not assigned
End Sub
End Class