2016-08-24 11 views
0

StoryBoardを使用してフェードインおよびフェードアウトする、私のホームページ(約30枚)に複数の画像を作成しようとしています。現時点では、アニメーションが1つの画像に完全に対応していますが、このように見えます。ストーリーボード中に画像のソースを変更する

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)を開始し、別のものを開始してください。 'AutoReverse'は' From'と 'To'を交換することで簡単に実現できます。 – Sinatr

+0

イメージを前のイメージにブレンドするか、現在のイメージをフェードアウトしてから、次のソースとフェードを変更しますか? – Clemens

+0

@Clemens後者のClemensは、完全にソースを変更し、新しいものを読み込みます。私は、ストーリーボードを完成した後にもう一度開始する方法を追加しましたが、うまくいきません。質問の中で私のコードを更新しました – CBreeze

答えて

0

次のような方法では、Imageコントロールをフェードアウトその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); 
    } 
} 
関連する問題