【问题标题】:Applying animated ScaleTransform in code problem在代码问题中应用动画 ScaleTransform
【发布时间】:2012-10-23 13:27:09
【问题描述】:

我试图找出为什么下面的代码似乎不起作用。它不会给出错误 - 它根本无法扩展。如果我将它更改为我的第二个代码示例,它实际上似乎确实有效。 有人知道吗?

谢谢

public static void StartMouseEnterAnimation(Button button)
    {
        Storyboard storyboard = new Storyboard();

        ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1);
        button.RenderTransformOrigin = new Point(0.5, 0.5);
        button.RenderTransform = scale;

        DoubleAnimation growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(300);
        growAnimation.From = 1;
        growAnimation.To = 1.8;
        storyboard.Children.Add(growAnimation);

        Storyboard.SetTargetProperty(growAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
        Storyboard.SetTarget(growAnimation, scale);

        storyboard.Begin();
    }

--- 以下确实有效,但我必须创建一个 TransformGroup 并通过更复杂的 PropertyChain 引用它...

public static void StartMouseEnterAnimation(Button button)
    {    
        Storyboard storyboard = new Storyboard();            
        ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1);
        button.RenderTransformOrigin = new Point(0.5, 0.5);
        TransformGroup myTransGroup = new TransformGroup();
        myTransGroup.Children.Add(scale);
        button.RenderTransform = myTransGroup;

        DoubleAnimation growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(100);
        //growAnimation.From = 1;
        growAnimation.To = 1.1;
        storyboard.Children.Add(growAnimation);

        DependencyProperty[] propertyChain = new DependencyProperty[]
        {
            Button.RenderTransformProperty, 
            TransformGroup.ChildrenProperty,
            ScaleTransform.ScaleXProperty
        };
        string thePath = "(0).(1)[0].(2)";
        PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain);
        Storyboard.SetTargetProperty(growAnimation, myPropertyPath);
        Storyboard.SetTarget(growAnimation, button);

        storyboard.Begin();
   }

【问题讨论】:

    标签: c# wpf animation rendertransform


    【解决方案1】:

    我可以通过像这样调整您的第一个代码示例来让它工作:

    public static void StartMouseEnterAnimation(Button button) {
        Storyboard storyboard = new Storyboard();
    
        ScaleTransform scale = new ScaleTransform(1.0, 1.0);
        button.RenderTransformOrigin = new Point(0.5, 0.5);
        button.RenderTransform = scale;
    
        DoubleAnimation growAnimation = new DoubleAnimation();
        growAnimation.Duration = TimeSpan.FromMilliseconds(300);
        growAnimation.From = 1;
        growAnimation.To = 1.8;
        storyboard.Children.Add(growAnimation);
    
        Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.ScaleX"));
        Storyboard.SetTarget(growAnimation, button);
    
        storyboard.Begin();
    }
    

    我使用new PropertyPath("RenderTransform.ScaleX")) 代替new PropertyPath(ScaleTransform.ScaleXProperty)),并将情节提要的目标设置为按钮(而不是scaleTransform 本身)。

    希望有帮助!

    【讨论】:

      【解决方案2】:

      下面是一个示例,说明当您有一个变换组时,如何在 ScaleTransform 上沿两个不同方向制作动画。路径字符串显示正在动画的部分。此外,由于 Canvas 是可冻结的,因此您必须 RegisterName。 (我不知道这是什么意思,但它是必需的)

              var storyBoard = new Storyboard();
              var group = new TransformGroup();
              var scale = new ScaleTransform(Zoom, Zoom);
              group.Children.Add(scale);
              group.Children.Add(new TranslateTransform(_translateX,_translateY));
              MainCanvas.RenderTransform = group;
      
              RegisterName("MainCanvas",MainCanvas);
      
              var growAnimation = new DoubleAnimation();
              growAnimation.Duration = TimeSpan.FromMilliseconds(1000);
              growAnimation.From = _oldZoom;
              growAnimation.To = Zoom;
              storyBoard.Children.Add(growAnimation);
      
              var growAnimation2 = new DoubleAnimation();
              growAnimation2.Duration = TimeSpan.FromMilliseconds(1000);
              growAnimation2.From = _oldZoom;
              growAnimation2.To = Zoom;
      
              storyBoard.Children.Add(growAnimation2);
      
              string thePath = "(0).(1)[0].(2)"; // Not used - just to show the syntax
      
      
              Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.Children[0].ScaleX"));
              Storyboard.SetTargetProperty(growAnimation2, new PropertyPath("RenderTransform.Children[0].ScaleY"));
              Storyboard.SetTargetName(growAnimation, "MainCanvas");
              Storyboard.SetTargetName(growAnimation2,"MainCanvas");
              storyBoard.Begin(this);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-24
        • 2023-03-25
        • 1970-01-01
        • 2015-10-11
        • 1970-01-01
        • 1970-01-01
        • 2017-10-31
        相关资源
        最近更新 更多