2014-04-15 93 views
1

我有一個簡單的Storyboard旋轉網格60度,然後回到0,但動畫沒有運行,我不知道爲什麼。我正在使用DispatcherTimer來確保它按設定的時間間隔運行。我也嘗試在xaml中創建Storyboard,並從Ticker事件中調用Storyboard.Begin(),但沒有運氣。提前致謝。 調試時,我添加一個斷點北京時間事件動畫運行動畫沒有運行在Windows 8.1應用程序使用DispatcherTimer

MainPage.cs

public sealed partial class MainPage : Page 
{ 
    private DispatcherTimer timer; 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     Loaded += (s, args) => 
     { 
      timer = new DispatcherTimer(); 

      timer.Tick += Ticker; 
      timer.Interval = new TimeSpan(0, 0, 6); 
      timer.Start();     
     }; 

    } 

    public void Ticker(object sender, object e) 
    { 
     var sb = new Storyboard(); 
     var animation2 = new DoubleAnimationUsingKeyFrames(); 
     var ease0 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = 0 }; 
     var ease1 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(2500)), Value = 40 }; 
     var ease2 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(4000)), Value = 60 }; 
     var ease3 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(4100)), Value = 0 }; 
     animation2.KeyFrames.Add(ease0); 
     animation2.KeyFrames.Add(ease1); 
     animation2.KeyFrames.Add(ease2); 
     animation2.KeyFrames.Add(ease3); 
     Storyboard.SetTargetProperty(animation2, "(UIElement.Projection).(PlaneProjection.RotationY)"); 
     Storyboard.SetTarget(animation2, grid2); 
     sb.Children.Add(animation2); 

     sb.Completed += (se, argss) => 
     { 

     }; 
     sb.Begin();    
    } 

MainPage.xaml中

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
     <Grid x:Name="grid2" Width="250" Height="250" Background="Blue" Grid.Column="1"> 
      <Grid.Projection> 
       <PlaneProjection/> 
      </Grid.Projection> 
     </Grid>  
</Grid> 

回答

0

希望這有助於。

您可以使用BeginTimeDuration來設置動畫的時間軸。

我已經使用begintime而不是timer.below代碼在此處正常工作。

protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     var sb = new Storyboard(); 
     var animation2 = new DoubleAnimationUsingKeyFrames(); 
     animation2.BeginTime = TimeSpan.FromMilliseconds(6); 
     var ease0 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = 0 }; 
     var ease1 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(2500)), Value = 40 }; 
     var ease2 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(4000)), Value = 60 }; 
     var ease3 = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(4100)), Value = 0 }; 
     animation2.KeyFrames.Add(ease0); 
     animation2.KeyFrames.Add(ease1); 
     animation2.KeyFrames.Add(ease2); 
     animation2.KeyFrames.Add(ease3); 
     Storyboard.SetTargetProperty(animation2, "(UIElement.Projection).(PlaneProjection.RotationY)"); 
     Storyboard.SetTarget(animation2, grid2); 
     sb.Children.Add(animation2); 

     sb.Completed += (se, argss) => 
     { 

     }; 
     sb.Begin(); 
    } 

您也可以使用xaml實現相同的動畫。

<Grid Background="Black"> 
    <Grid x:Name="grid2" Width="250" Height="250" Background="Blue" Grid.Column="1"> 
     <Grid.Projection> 
      <PlaneProjection/> 
     </Grid.Projection> 
     <Grid.Triggers> 
      <EventTrigger> 
       <BeginStoryboard> 
        <Storyboard> 
         <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="grid2"> 
          <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0"/> 
          <EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="40"/> 
          <EasingDoubleKeyFrame KeyTime="0:0:0.9" Value="60"/> 
          <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0"/> 
         </DoubleAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Grid.Triggers> 
    </Grid> 
</Grid> 
+0

感謝您的回答。這並不能解決使用DispatcherTimer以設定間隔運行故事板的問題,儘管它只運行一次。 – SWilko

相關問題