问题
i'm trying to do an animation before the visibility changes to collapsed.
I'm trying to accomplish a fade in/fade out effect when the visibility is changed. I have no problem with fade in, because the visibility changes before my animation (which is good).
Here's my code right now:
private void LoginOverlay_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.Visibility == Visibility.Visible)
{
//Fade in ... this is working
Storyboard sb = new Storyboard();
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = 1;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
sb.Children.Add(da);
Storyboard.SetTargetProperty(da, new PropertyPath(OpacityProperty));
Storyboard.SetTarget(da, this);
sb.Begin();
}
else
{
//Fade out ... not working
Storyboard sb = new Storyboard();
DoubleAnimation da = new DoubleAnimation();
da.From = 1;
da.To = 0;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
sb.Children.Add(da);
Storyboard.SetTargetProperty(da, new PropertyPath(OpacityProperty));
Storyboard.SetTarget(da, this);
sb.Begin();
}
}
回答1:
Before you start the Animation the visibility of the control has already changed to Collapsed or Hidden, this means that the opacity will animate but you won't be able to see it happen as the control is invisible.
One option you have is to change the control back to visible before starting the animation, then add a keyframe animation to the storyboard to set the visibility back to its intended value after 1 second (or the length of your fade animation)
来源:https://stackoverflow.com/questions/26932888/do-something-before-the-visibilty-is-changed-in-wpf