一些代碼,看看發生了什麼事情: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吧。我在文檔中沒有遇到類似的東西,或者我錯過了它。對於獨立動畫,沒有提到過那種情況。我嘗試了不同的東西和方法,但沒有任何結果。
這只是一個評論,爲什麼不把'element.Opacity = 0;'移到'OpacityWithChildren_Light'的foreach循環? –
@JeroenvanLangen UIElement會立即變成動畫,我希望它們都是隱形的,然後出現。 –