2016-08-24 3 views
0

StoryBoard을 사용하여 페이드 인하거나 페이드 아웃하는 내 홈 페이지 (약 30 개)에 여러 개의 이미지가있는 것을 시도하고 있습니다. 애니메이션에서 한 장면을 완벽하게 처리하는 순간, 이렇게 보입니다.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) 뭔가를 시작하고 다른 것을 시작하십시오. '자동 반전'은 '보낸 사람'과 '받는 사람'을 교환하면 쉽게 달성 할 수 있습니다. – 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); 
    } 
}