+1  A: 

Sounds like you're looking for something like this:

public static T FadeIn<T>(this T uiElement, int durationInSeconds)
{
  return uiElement.FadeFromTo(0, 1, durationInSeconds, false);
}
public static T FadeOut<T>(this T uiElement, int durationInSeconds)
{
  return uiElement.FadeFromTo(1, 0, durationInSeconds, false);
}

public static T FadeFromTo<T>(this T uiElement,
                              double fromOpacity, double toOpacity,
                              int durationInSeconds, bool loopAnimation)
where T : UIElement
{
  return (T)uiElement.wpfInvoke(()=>
  {
    var doubleAnimation =
      new DoubleAnimation(fromOpacity, toOpacity,
                          new Duration(TimeSpan.FromSeconds(durationInSeconds)));
    if(loopAnimation)
      doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
    uiElement.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);
    return uiElement;
   });
}
Ray Burns
Sweet, thanks I knew that there should be an easier way :)I works ok, except when I try to run one after the other (but I think that that might be some threading issue)
Dinis Cruz
It is not a threading issue. Perhaps you are looking for Compose behavior. If so, pass `HandoffBehavior.Compose` as a third argument to BeginAnimation. On the other hand, perhaps you want to animate from the current value, in which case you should leave the default HandoffBehavior alone (it defaults to `SnapshotAndReplace`) but pass in null for "fromOpacity" in FadeIn and FadeOut. Note: To accept nulls, FadeFromTo needs to take arguments of type `double?` and construct the DoubleAnimation thusly: `new DoubleAnimation { From = fromOpacity, To=toOpacity, Duration = ... };`
Ray Burns