Programatically WPF Fade In (via extension methods)

有些话、适合烂在心里 提交于 2020-01-16 19:09:42

问题


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;
                });
}

回答1:


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;
   });
}



回答2:


Here is the solution for the Grid.

Where the Grid is **<Grid Name="RootGrid" Opacity="0">**

 this.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
            {
                var doubleAnimation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(5)));
                RootGrid.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);
            }));


来源:https://stackoverflow.com/questions/3049669/programatically-wpf-fade-in-via-extension-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!