WP7 - Using the storyboard defined in Application.Resources

In my Application.Resources , I have the following Storyboard .

 <Application.Resources> <!--Storyboard animation for fading out a UI element--> <Storyboard x:Key="FadeOutAnimation"> <DoubleAnimation From="1" To="0" Duration="0:0:0.25" Storyboard.TargetProperty="Opacity" AutoReverse="False" /> </Storyboard> </Application.Resources> 

In encoding, I use this to extinguish some TextBlock when the user clicks on them.

 // Get the storyboard from application resources Storyboard sb = (Storyboard)App.Current.Resources["FadeOutAnimation"]; // Setup the animation target for fade out Storyboard.SetTarget( sb.Children.ElementAt( 0 ) as DoubleAnimation, myTextBlock ); // Set the animation completed handler sb.Completed += ( s, e1 ) => { // Stop the Storyboard sb.Stop(); // Hide the TextBlock myTextBlock.Visibility = Visibility.Collapsed; }; // Start the Storyboard sb.begin(); 

The question is, does myTextBlock need to be โ€œ myTextBlock โ€ from the DoubleAnimation target DoubleAnimation ?

If so, how do I do this?

The reason I ask, I am worried that the link to the TextBlock hangs until this storyboard is used again.

Thank you for your help!

+6
windows-phone-7 wpf silverlight
source share
2 answers

We donโ€™t always need to use Xaml in sliverlight if it is in our path: -

  public static AnimationHelper { public static void FadeOutAndCollapse(UIElement target) { DoubleAnimation da = new DoubleAnimation(); da.From = 1.0; da.To = 0.0; da.Duration = TimeSpan.FromSeconds(0.25); da.AutoReverse = false; StoryBoard.SetTargetProperty(da, new PropertyPath("Opacity")); StoryBoard.SetTarget(da, target); StoryBoard sb = new StoryBoard(); sb.Children.Add(da); EventHandler eh = null; eh = (s, args) => { target.Visiblity = Visibility.Collapsed; sb.Stop(); sb.Completed -= eh; } sb.Completed += eh; sb.Begin(); } } 

In this case, you can display and collapse any user interface element with: -

 AnimationHelper.FadeOutAndCollapse(myTextBox); 

I was inclined to remove From = 1.0 to make it more general, so that elements having a lower initial opacity would not suddenly flash until they were completely opaque before disappearing.

+12
source share

Don't worry about dangling links to light user interface elements; they will collect garbage when there are no more links. Your more pressing problem is that one storyboard is used for several text objects, and if they intersect, this will not be done.

For example, if you start one animation and then start another, then both of them will stop at the same time, because there is only one storyboard, and your handler will cause it to stop. Either connect a separate storyboard to each text element in XAML, or create a new storyboard in the code for each animation you want to make.

In addition, if you must use one storyboard, you must be careful to delete the processed event handler, because for now you will continue to accumulate them, and old handlers will be called when the storyboard is complete.

0
source share

All Articles