2016-12-05 4 views
0

일부 코드를 작동합니다DoubleAnimation가에 무슨 일이 일어나고 있는지보고 한 번만

XAML 파일 :

<!-- put this code into your page xaml code--> 

<Page.Resources> 
    <Style x:Name="opabuttonstyle" TargetType="Button"> 
     <Setter Property="Margin" Value="10" /> 
     <Setter Property="HorizontalAlignment" Value="Center" /> 
     <Setter Property="VerticalAlignment" Value="Center" /> 
    </Style> 
</Page.Resources> 

<Grid Background="{StaticResource BackgroundGradient-Blue}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <Grid Grid.Row="0" HorizontalAlignment="Center"> 
     <Button 
      x:Name="testingIsTheFutureButton" 
      Grid.Row="0" 
      Grid.Column="0" 
      Click="testingIsTheFutureButton_Click" 
      Content="Opacity Animation" 
      Style="{StaticResource opabuttonstyle}" /> 
    </Grid> 
    <Grid Grid.Row="1"> 
     <StackPanel x:Name="stackingStackPanel" HorizontalAlignment="Center"> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
      <TextBlock Text="Element" /> 
     </StackPanel> 
    </Grid> 
</Grid> 

클래스 파일 SingleAnimations.cs :

// This function takes UIElement and DoubleAnimate it to fade in. 
public static void SimpleElementFadeIn(object sender, int fadeTime, int delay) 
{ 
     UIElement element = sender as UIElement; //bierz element 
     Storyboard storyboard = new Storyboard(); 
     DoubleAnimation doubleAnimation = new DoubleAnimation(); 
     doubleAnimation.BeginTime = new TimeSpan(0, 0, 0, 0, delay); 
     doubleAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, fadeTime)); 
     doubleAnimation.From = 0; 
     doubleAnimation.To = 1; 
     Storyboard.SetTarget(doubleAnimation, element); 
     Storyboard.SetTargetProperty(doubleAnimation, "Opacity"); 
     storyboard.Children.Add(doubleAnimation); 
     storyboard.Begin(); 
} 

클래스 파일 CollectionAnimations.cs :

// This function goes through every children element in UIElement (grid, 
//stackpanel or whatever we send there) and animates it using previous function 
public static void OpacityWithChildren_Light(object sender, int delay) 
{ 
    SetOpacityToZero_Deep(sender); // set opacity to 0 to all elements before animation 
    UIElement rootElement = sender as UIElement; 
    int childCount= VisualTreeHelper.GetChildrenCount(rootElement); 
    if (childCount > 0) 
    { 
     for (int index = 0; index < childCount; index++) 
     { 
       UIElement element = (UIElement)VisualTreeHelper.GetChild(rootElement, index); 
       SingleAnimations.SimpleElementFadeIn(element, 100, delay * index); 
     } 
    } 
} 

// This function goes through every children element like the funtion above 
// and sets opacity of these elements to 0 (so every element is invisible before it's animated) 
public static void SetOpacityToZero_Deep(object sender) 
{ 
    UIElement rootElement = sender as UIElement; 
    int childNumber = VisualTreeHelper.GetChildrenCount(rootElement); 
    if (childNumber > 0) 
    { 
     for (int index = 0; index < childNumber; index++) 
     { 
      UIElement element = (UIElement)VisualTreeHelper.GetChild(rootElement, index); 
       element.Opacity = 0; 
     } 
    } 
} 

내가 button_click 이벤트에

CollectionAnimations.OpacityWithChildren_Light(stackingStackPanel, 50); 

를 호출 애니메이션을 시작합니다.

는 내가 기대하는 것은 StackPanel의에서 TextBlocks 모두 사라지고 하나 하나에 페이드 예정이다. 이것은 처음 사용했을 때만 발생합니다. 버튼을 다시 클릭하면 모든 항목이 표시되고 하나씩 애니메이션이 적용됩니다. 메인 페이지으로 돌아가서 페이지를 다시 입력하면 이전처럼 다시 작동합니다.

어쩌면 어떤 종류의 캐쉬를 청소해야합니다. 나는 문서에서 그와 같은 것을 보지 않았거나 방금 그것을 놓쳤다. 그와 같은 것은 독립된 애니메이션에서 언급되지 않았습니다. 나는이 코드를 다른 것들과 접근법을 시도하면서 엉망으로 만들었지 만, 아무 것도 효과가 없었다.

+0

이것은 단지 코멘트 일 뿐이므로 'OpacityWithChildren_Light'의 foreach 루프로 'element.Opacity = 0;'을 이동하지 않으시겠습니까? –

+0

@ JeroenvanLangen UIElement는 즉시 애니메이션으로 표시되며 모든 요소가 보이지 않게 나타나고 표시됩니다. –

답변

1

두 개의 스토리 보드를 사용하여 Opacity을 0으로 설정하고 다른 하나는 원하는대로 DoubleAnimation을 재생할 수 있어야한다고 제안합니다.

따라서 OpacityUIElement.Opacity으로 0으로 설정할 필요가 없습니다. 제로로 설정 OpacityStoryboard가 완료되면, 우리는 페이드의 Storyboard을 시작할 수있을 것입니다 예를 들어

:.

SingleAnimation 클래스 :

internal class SingleAnimations 
{ 
    public static DoubleAnimation SetToZero(object sender) 
    { 
     var element = sender as UIElement; 
     DoubleAnimation doubleAnimation = new DoubleAnimation(); 
     doubleAnimation.BeginTime = new TimeSpan(0, 0, 0, 0, 0); 
     doubleAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 0)); 
     doubleAnimation.From = 1; 
     doubleAnimation.To = 0; 
     Storyboard.SetTarget(doubleAnimation, element); 
     Storyboard.SetTargetProperty(doubleAnimation, "Opacity"); 
     return doubleAnimation; 

    } 

    public static DoubleAnimation SimpleElementFadeIn(object sender, int fadeTime, int delay) 
    { 
     var element = sender as UIElement; 
     DoubleAnimation doubleAnimation = new DoubleAnimation(); 
     doubleAnimation.BeginTime = new TimeSpan(0, 0, 0, 0, delay); 
     doubleAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, fadeTime)); 
     doubleAnimation.From = 0; 
     doubleAnimation.To = 1; 
     Storyboard.SetTarget(doubleAnimation, element); 
     Storyboard.SetTargetProperty(doubleAnimation, "Opacity"); 
     return doubleAnimation; 

    } 

OpacityWithChildren_Light 방법 :

public static void OpacityWithChildren_Light(object sender, int delay) 
{ 
    var storyboardOfSetOpacityToZero = new Storyboard(); 
    var storyboardFadeIn = new Storyboard(); 
    UIElement rootElement = sender as UIElement; 
    int childCount = VisualTreeHelper.GetChildrenCount(rootElement); 
    if (childCount > 0) 
    { 
     for (int index = 0; index < childCount; index++) 
     { 
      UIElement element = (UIElement)VisualTreeHelper.GetChild(rootElement, index); 
      var SetOpacityToZero = SingleAnimations.SetToZero(element); 
      var SetOpacityToOne = SingleAnimations.SimpleElementFadeIn(element, 100, delay * index); 
      storyboardOfSetOpacityToZero.Children.Add(SetOpacityToZero); 
      storyboardFadeIn.Children.Add(SetOpacityToOne); 
     } 
     storyboardOfSetOpacityToZero.Begin(); 
     storyboardOfSetOpacityToZero.Completed += (s, e) => { storyboardFadeIn.Begin(); }; 
    } 
} 
+0

고마워요! 이 코드는 내 방식보다 뛰어나며 매력처럼 작동합니다. 여전히 불투명도가 0으로 설정되도록 애니메이션되어야하고 다른 방식으로 작동하지 않는 이유는 흥미 롭습니다. –

관련 문제