Feedback

TextBlock animiert ein- und ausblenden

Sprache: C#

Nachdem ich gestern bei einem Film die Einblendungen der „credits“ im Vorspann sah, dachte ich mir : „Das geht doch bestimmt auch ganz einfach mit WPF“. Habe mich dann auch dran gesetzt und eine kleine Extension-Methode geschrieben, mit welcher man jeden beliebigen TextBlock um eine „credits“-ähnliche Animation erweitern kann. Die Extension-Methode sieht folgendermaßen aus : [code]public static void AddSmartFadingAnimation( this TextBlock TextBlockToAnimate, List<string> FadingTextList, double TextPromptDuration, double FadingSpeed ) [/code] Die Verwendung der Methode ist recht einfach. Man legt einfach ein TextBlock-Control in seiner GUI an, bestimmt alle Eigenschaften (wie der TextBlock auszusehen hat), erstellt ein String-Array (List<sring>), füllt dieses mit den entsprechenden Texten (.add(„Text“)) und wendet die Methode [b]AddSmartFadingAnimation(…)[/b] auf dem TextBlock-Objekt an. Eine beispielhafte Verwendung der Methode sieht folgendermaßen aus : [code]List<string> TextList = new List<string>(); for( int i = 0; i < 10; i++ ) TextList.Add( "Einzublendender Text Nr. " + i ); tblFadingInfo.AddSmartFadingAnimation( TextList, 1000, 1500 ); [/code] (tblFadingInfo ist hierbei der Name eines TextBlock-Objektes) Weiterführende Informationen (Screenshots und Beispielapplikation zum Download) erhaltet ihr in meinem Blog unter : [u]http://dotnet-forum.de/blogs/thearchitect/[/u]
	public static class SmartTextAnimation
	{
		/// <summary>
		/// Add a fading animation to a 'TextBlock'-Control
		/// </summary>
		/// 
		/// <param name="FadingTextList">The generic List of all strings that have to be shown</param>
		/// <param name="TextPromptDuration">The duration of the text prompt in milliseconds</param>
		/// <param name="FadingSpeed">The speed of the text fading animation in milliseconds</param>
		/// 
		/// <returns></returns>
		/// 
		public static void AddSmartFadingAnimation( this TextBlock TextBoxToAnimate, List<string> FadingTextList,
																							  double TextPromptDuration, double FadingSpeed )
		{
			int index = 0;
			DoubleAnimation FadeOutAnimation = new DoubleAnimation()
			{
				Duration = new Duration( TimeSpan.FromMilliseconds( FadingSpeed ) ),
				To = 0, BeginTime = TimeSpan.FromMilliseconds( TextPromptDuration )
			};
			DoubleAnimation FadeInAnimation = new DoubleAnimation()
			{
				Duration = new Duration( TimeSpan.FromMilliseconds( FadingSpeed ) ),
				To = 1, BeginTime = TimeSpan.FromMilliseconds( TextPromptDuration + FadingSpeed )
			};

			Storyboard FadeInAndOutStoryboard = new Storyboard() { RepeatBehavior = RepeatBehavior.Forever };
			Storyboard.SetTargetProperty( FadeOutAnimation, new PropertyPath( "(Opacity)" ) );
			Storyboard.SetTargetProperty( FadeInAnimation, new PropertyPath( "(Opacity)" ) );
			Storyboard.SetTarget( FadeOutAnimation, TextBoxToAnimate );
			Storyboard.SetTarget( FadeInAnimation, TextBoxToAnimate );

			FadeInAndOutStoryboard.Children.Add( FadeOutAnimation );
			FadeInAndOutStoryboard.Children.Add( FadeInAnimation );

			TextBoxToAnimate.Text = FadingTextList[index++];

			FadeOutAnimation.CurrentStateInvalidated += new EventHandler( (sender, eArgs) =>
			{
				if ( (sender as Clock).CurrentState == ClockState.Filling )
				{
					TextBoxToAnimate.Text = FadingTextList[index];
					if (++index == FadingTextList.Count) index = 0;
				}
			} );

			FadeInAndOutStoryboard.Begin();
		}
	}
	public static class SmartTextAnimation
	{
		/// <summary>
		/// Add a fading animation to a 'TextBlock'-Control
		/// </summary>
		/// 
		/// <param name="FadingTextList">The generic List of all strings that have to be shown</param>
		/// <param name="TextPromptDuration">The duration of the text prompt in milliseconds</param>
		/// <param name="FadingSpeed">The speed of the text fading animation in milliseconds</param>
		/// 
		/// <returns></returns>
		/// 
		public static void AddSmartFadingAnimation( this TextBlock TextBoxToAnimate, List<string> FadingTextList,
																							  double TextPromptDuration, double FadingSpeed )
		{
			int index = 0;
			DoubleAnimation FadeOutAnimation = new DoubleAnimation()
			{
				Duration = new Duration( TimeSpan.FromMilliseconds( FadingSpeed ) ),
				To = 0, BeginTime = TimeSpan.FromMilliseconds( TextPromptDuration )
			};
			DoubleAnimation FadeInAnimation = new DoubleAnimation()
			{
				Duration = new Duration( TimeSpan.FromMilliseconds( FadingSpeed ) ),
				To = 1, BeginTime = TimeSpan.FromMilliseconds( TextPromptDuration + FadingSpeed )
			};

			Storyboard FadeInAndOutStoryboard = new Storyboard() { RepeatBehavior = RepeatBehavior.Forever };
			Storyboard.SetTargetProperty( FadeOutAnimation, new PropertyPath( "(Opacity)" ) );
			Storyboard.SetTargetProperty( FadeInAnimation, new PropertyPath( "(Opacity)" ) );
			Storyboard.SetTarget( FadeOutAnimation, TextBoxToAnimate );
			Storyboard.SetTarget( FadeInAnimation, TextBoxToAnimate );

			FadeInAndOutStoryboard.Children.Add( FadeOutAnimation );
			FadeInAndOutStoryboard.Children.Add( FadeInAnimation );

			TextBoxToAnimate.Text = FadingTextList[index++];

			FadeOutAnimation.CurrentStateInvalidated += new EventHandler( (sender, eArgs) =>
			{
				if ( (sender as Clock).CurrentState == ClockState.Filling )
				{
					TextBoxToAnimate.Text = FadingTextList[index];
					if (++index == FadingTextList.Count) index = 0;
				}
			} );

			FadeInAndOutStoryboard.Begin();
		}
	}