2012-12-22 47 views
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 
+0

抱歉,我想說,我想能夠在文本框中寫入持續時間...... – Ste

回答

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 
+0

我認爲這是在#C對不對? – Ste

+0

我會嘗試適應它在VB讓我們看看它是否有效 – Ste

+0

我試過這個,但它不起作用:Public Sub Set_Laptime(ByVal sender,ByVal KeyUpEventArgs) Dim ts As TimeSpan Dim LapTime As Duration ts =(TextBox1 .Text) LapTime =新的持續時間(ts) End Sub – Ste

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屬性。

首先改變你DoubleAnimations在XAML中,以這樣的:

<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 
+0

好的我試過這最後的代碼,但它不起作用。持續時間總是取決於XAML上設置的內容,我在xaml跟蹤窗口中有我的文本框,我應該對此做些什麼? – Ste

+0

另外我認爲我的動畫時間格式是00:00:00這意味着小時:分:秒 – Ste

+0

這很好,但你希望你的用戶輸入它爲00:00:00。你也需要一個依賴屬性來做你想做的事情,我現在正在研究 –