2

我有一个双重动画,我知道我可以使用: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
4

2 回答 2

0

如果我理解正确,是的。您只需要处理KeyUp 事件 ,例如,如果您想在输入持续时间并按下 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 转换(由本网站):

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
于 2012-12-22T07:04:34.097 回答
0

看起来如果您只是从默认为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
于 2012-12-22T19:04:26.467 回答