我有一个双动画我知道我可以使用:Duration="00:00:30"
来设置持续时间,但是当文件在开始动画之前运行时,我可以用Textbox
写入持续时间值
基本上我需要的是Duration=" Time written within the TextBox"
这就是我试过的
Public Sub Set_Laptime(ByVal sender, ByVal KeyUpEventArgs, ByVal e)
Dim ts As TimeSpan
Dim LapTime As Duration
ts = (TextBox1.Text)
LapTime = New Duration(ts)
End Sub
答案 0 :(得分:0)
如果我理解正确,是的。你只需要处理KeyUp event 例如,如果要在输入持续时间并按下Return键时开始动画:
void textBox_KeyUp(object sender, KeyUpEventArgs e)
{
if (e.Key==key.return)
{
TimeSpan ts=TimeSpan.Parse(textBox.Text);
Duration dur=new Duration(ts);
}
}
修改强> VB转换(this site):
Private Sub textBox_KeyUp(ByVal sender As Object, ByVal e As KeyUpEventArgs)
If (e.Key = key.return) Then
Dim ts As TimeSpan = TimeSpan.Parse(textBox.Text)
Dim dur As Duration = New Duration(ts)
End If
End Sub
答案 1 :(得分:0)
如果您只是从字符串创建TimeSpan,则默认为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>
然后在您的usercontrol中将您的代码更改为:
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