代码之家  ›  专栏  ›  技术社区  ›  Dinis Cruz

编程WPF淡入(通过扩展方法)

  •  0
  • Dinis Cruz  · 技术社区  · 16 年前

    这里是我想创建的API方法类型的一个示例,仅供参考。此代码将根据提供的值(fromValue、toValue、duration和loop)旋转UIElement

    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;
                    });
    }
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   Ray Burns    16 年前

    听起来你在找这样的东西:

    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
  •  1
  •   NoWar    14 年前

    下面是网格的解决方案。

    网格在哪里 **<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);
                }));