일부 코드를 작동합니다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 모두 사라지고 하나 하나에 페이드 예정이다. 이것은 처음 사용했을 때만 발생합니다. 버튼을 다시 클릭하면 모든 항목이 표시되고 하나씩 애니메이션이 적용됩니다. 메인 페이지으로 돌아가서 페이지를 다시 입력하면 이전처럼 다시 작동합니다.
어쩌면 어떤 종류의 캐쉬를 청소해야합니다. 나는 문서에서 그와 같은 것을 보지 않았거나 방금 그것을 놓쳤다. 그와 같은 것은 독립된 애니메이션에서 언급되지 않았습니다. 나는이 코드를 다른 것들과 접근법을 시도하면서 엉망으로 만들었지 만, 아무 것도 효과가 없었다.
이것은 단지 코멘트 일 뿐이므로 'OpacityWithChildren_Light'의 foreach 루프로 'element.Opacity = 0;'을 이동하지 않으시겠습니까? –
@ JeroenvanLangen UIElement는 즉시 애니메이션으로 표시되며 모든 요소가 보이지 않게 나타나고 표시됩니다. –