2016-12-05 63 views
0

一些代碼,看看發生了什麼事情:DoubleAnimation是使用VisualTreeHelper只能一次

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; 
     } 
    } 
} 

開始動畫我叫

CollectionAnimations.OpacityWithChildren_Light(stackingStackPanel, 50); 

button_click事件。

我想到的是,所有在的StackPanel的TextBlocks的消失,會由一個在一個褪色。這種情況正在發生,但僅限於首次使用。如果我再次點擊按鈕,那麼它們都是可見的,並且一個一個地動畫 - 而不是我想要發生的事情。如果我回到MainPage並再次進入該頁面,它將再次(一次)像以前一樣工作。

我應該清理某種cashe吧。我在文檔中沒有遇到類似的東西,或者我錯過了它。對於獨立動畫,沒有提到過那種情況。我嘗試了不同的東西和方法,但沒有任何結果。

+0

這只是一個評論,爲什麼不把'element.Opacity = 0;'移到'OpacityWithChildren_Light'的foreach循環? –

+0

@JeroenvanLangen UIElement會立即變成動畫,我希望它們都是隱形的,然後出現。 –

回答

1

我建議你應該可以使用兩個故事板來做到這一點,一個將Opacity設置爲0,另一個可以根據需要播放DoubleAnimation

所以我們不需要通過UIElement.OpacityOpacity設置爲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,並且不能以其他方式工作。 –