I'm trying to learn about attached behaviors in WPF/WP7 Silverlight, and I've come up against a conceptual roadblock that I wondered if someone could weigh in on.
An example of the functionality I'm trying to create is the following: The user clicks on a button whose Click handler triggers navigation (i.e. NavigationContext.Navigate()), and prior to the navigation actually occurring, an animation occurs on the button that reduces its opacity from 1 to 0 (a "fade out" effect).
So, in order:
- The user clicks the button
- The button executes an animation
- The navigation occurs
Simple enough, right? I created an attached behavior that looks like so:
public class FadeBehavior : Behavior<Button>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Click += (obj, args) => {
Storyboard sb = new Storyboard();
DoubleAnimation an = new DoubleAnimation();
an.Duration = TimeSpan.FromMilliseconds(300);
an.From = 1;
an.To = 0;
Storyboard.SetTarget(an, AssociatedObject);
Storyboard.SetTargetProperty(an, new PropertyPath("Opacity"));
sb.Children.Add(an);
sb.Begin();
};
}
}
This obviously works fine if the .xaml.cs of the page the button's on doesn't contain a Click handler that triggers navigation, but when I wire that up, the animation doesn't complete before the navigation occurs. I understand why this is, of course, but I'm wondering if there's a method to accomplish this that also exhibits the wonderful property of non-terrible design.
I found this SO thread that is seemingly related, but is the accepted answer REALLY the only way to do this?
Thanks for your help.
EDIT: If you happen to have a WP7 and are hell-bent on figuring this out, the Reddit reader app "Baconit" exhibits a behavior similar to the one I'm interested in when you click on the title of a story.