код, чтобы увидеть, что происходит: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 события.
Что я ожидаю, что все TextBlocks в StackPanel исчезают и собираются исчезать один за другим. Это происходит, но только при первом использовании. Если я снова нажму кнопку, то все они будут видны и будут анимированы один за другим - не то, что я хочу. Если я вернусь к MainPage и снова введите страницу, она будет работать снова (один раз), как раньше.
Должно быть, я должен очистить что-то вроде cashe. Я не встречал ничего подобного в документации или просто пропустил это. Ничего подобного не упоминалось для независимых анимаций. Я испортил этот код, пробуя разные вещи и подходы, но ничего не получилось.
Это всего лишь комментарий, почему бы не переместить 'element.Opacity = 0;' в цикл foreach 'OpacityWithChildren_Light'? –
@JeroenvanLangen UIElement будет мгновенно анимирован, и я хочу, чтобы все они были невидимыми, а затем отображались. –