在代码问题中应用动画 ScaleTransform

发布于 2024-08-19 02:09:37 字数 2084 浏览 5 评论 0原文

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

谢谢

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

I am trying to find out why the code below does not seem to work. It does not give an error - it simply doesn't scale. It actually does seem to work if I change it as to my second code sample.
Anyone got any idea?

Thanks

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

--- The following DOES work but I had to create a TransformGroup and reference this through a more complicated 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();
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2024-08-26 02:09:37

我可以通过调整您的第一个代码示例来使其工作,如下所示:

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本身)。

希望有帮助!

I was able to get it to work by tweaking your first code sample like so:

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

Instead of new PropertyPath(ScaleTransform.ScaleXProperty)), I used new PropertyPath("RenderTransform.ScaleX")), and I set the target of the storyboard to the button (not the scaleTransform itself).

Hope that helps!

沉睡月亮 2024-08-26 02:09:37

以下是当您有变换组时如何在 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);

Here is an example of how to animate in two different directions on a ScaleTransform, when you have a transform group. The path string shows which part is being animated. Also, because Canvas is freezable, you have to RegisterName. (I don't know what this means, but it is required)

        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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文