Programatically WPF Fade In (via extension methods)
- by Dinis Cruz
I'm trying to write a simple (stand alone) C# extension method to do a Fade-In of a generic WPF UIElement, but the solutions (and code samples) that I found all contain a large number of moving parts (like setting up a story, etc...)
For reference here is an example of the type of API method I would like to create. This code will rotate an UIElement according to the values provided (fromValue, toValue, duration and loop)
public static T rotate<T>(this T uiElement, double fromValue, double toValue, int durationInSeconds, bool loopAnimation)
where T : UIElement
{
return (T)uiElement.wpfInvoke(
()=>{
DoubleAnimation doubleAnimation = new DoubleAnimation(fromValue, toValue, new Duration(TimeSpan.FromSeconds(durationInSeconds)));
RotateTransform rotateTransform = new RotateTransform();
uiElement.RenderTransform = rotateTransform;
uiElement.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
if (loopAnimation)
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
rotateTransform.BeginAnimation(RotateTransform.AngleProperty, doubleAnimation);
return uiElement;
});
}