2016-08-24 44 views
0

我試圖在我的主頁(大約30)上使用StoryBoard淡入淡出的多個圖像。目前我的動畫在一張照片上完美運行,看起來像這樣;在StoryBoard中更改圖像源

private void OnDashboardPageLoaded(object sender, RoutedEventArgs e) 
{ 
    Storyboard storyboard = new Storyboard(); 
    TimeSpan duration = new TimeSpan(0, 0, 10); 

    // Create a DoubleAnimation to fade the not selected option control 
    DoubleAnimation animation = new DoubleAnimation(); 

    animation.From = 0.0; 
    animation.To = 1.0; 
    animation.Duration = new Duration(duration); 

    // Configure the animation to target de property Opacity 
    Storyboard.SetTargetName(animation, testImage.Name); 
    Storyboard.SetTargetProperty(animation, new PropertyPath(OpacityProperty)); 

    // Add the animation to the storyboard 
    storyboard.Children.Add(animation); 

    storyboard.RepeatBehavior = RepeatBehavior.Forever; 
    animation.AutoReverse = true; 

    // Begin the storyboard 
    storyboard.Begin(this); 
} 

而我的XAML;

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <Image Source="http://www.online-image-editor.com/styles/2013/images/example_image.png" x:Name="testImage" Stretch="Uniform" /> 
</Grid> 

正如你可以看到我有一個靜態源在這裏設置,但最終我想要做的是加載所有的圖像在一個目錄,並在每個動畫結束更改源,這樣一個新的每次顯示圖像(直到全部顯示30),我該怎麼做?

編輯:完成故事板;

storyboard.Completed += new EventHandler(Story_Completed); // In OnDashboardPageLoaded method 

private void Story_Completed(object sender, EventArgs e) 
{ 
    storyboard.Begin(this); 
} 
+0

*「更改每個動畫結束時的源代碼」* - 聽起來你不想再使用RepeatBehavior.Forever。相反,開始單一動畫/故事板和[當它完成](http://stackoverflow.com/a/7333462/1997232)做一些事情,並開始另一個。通過交換'From'和'To'可以輕鬆實現'AutoReverse'。 – Sinatr

+0

你想要在前一個圖像上混合一個圖像,或者只是淡出當前圖像,然後更改源並在下一個圖像中淡入? – Clemens

+0

@Clemens後者Clemens,一個完全淡出,源變化,新的加載。我已經添加了一個方法,在它剛剛啓動故事板後,它已完成,但它不工作...我已經更新我的代碼在 – CBreeze

回答

0

以下方法淡出一個圖像控制,改變其Source屬性,並再次消失它英寸您可以用另一個ImageSource參數循環調用它(例如,由DispatcherTimer控制)。

public static void ShowNextImage(Image image, ImageSource source, TimeSpan fadeTime) 
{ 
    if (image.Source == null) // no fade out 
    { 
     image.Source = source; 
     image.BeginAnimation(UIElement.OpacityProperty, 
      new DoubleAnimation(0d, 1d, fadeTime)); 
    } 
    else 
    { 
     var fadeOut = new DoubleAnimation(0d, fadeTime); 

     fadeOut.Completed += (s, e) => 
     { 
      image.Source = source; 
      image.BeginAnimation(UIElement.OpacityProperty, 
       new DoubleAnimation(1d, fadeTime)); 
     }; 

     image.BeginAnimation(UIElement.OpacityProperty, fadeOut); 
    } 
}