简体   繁体   English

在WPF中,我看不到C#故事板和DoubleAnimation有什么问题

[英]In WPF I can't see whats wrong with my C# Storyboard and DoubleAnimation

Im attempting to create a storyboard in C# not XAML to control scaling of an image, so i can easily alter the ScaleTransform.ScaleX and ScaleTransform.ScaleY values in a DoubleAnimation . 我试图用C#而不是XAML创建一个故事板来控制图像的缩放,因此我可以轻松地在DoubleAnimation更改ScaleTransform.ScaleXScaleTransform.ScaleY值。

So far I believe i have created the animations and added it to a new storyboard, and the appropriate values change in the C# when i check with breakpoints, but it's not actually working. 到目前为止,我相信我已经创建了动画并将其添加到新的情节提要中,并且当我使用断点进行检查时,C#中的适当值会更改,但实际上并没有用。

My C# looks like this: 我的C#看起来像这样:

    public void SetStatistics(double[] value)
    {
        Storyboard sb = new Storyboard();
        sb.Duration = new Duration(TimeSpan.FromSeconds(1));

        //Wedge Animation X-Axis    
        DoubleAnimation wax = new DoubleAnimation();
        //Wedge Animation Y-Axis  
        DoubleAnimation way = new DoubleAnimation();

        ScaleTransform st = ((ScaleTransform)FindName("wedge1scale"));

        wax = new DoubleAnimation();
        way = new DoubleAnimation();
        wax.Duration = sb.Duration;
        way.Duration = sb.Duration;

        sb.Children.Add(wax);
        sb.Children.Add(way);

        Storyboard.SetTargetProperty(wax, new PropertyPath("(ScaleTransform.ScaleX)"));

        //End scale from calculation with an Enum value
        wax.To = StatMin + (StatPercent * value[1]);
        //Start scale from current value
        wax.From = st.ScaleX;

        Storyboard.SetTargetProperty(way, new PropertyPath("(ScaleTransform.ScaleY)"));

        //End scale from calculation with an Enum value
        way.To = StatMin + (StatPercent * value[1]);
        //Start scale from current value
        way.From = st.ScaleY;

        Storyboard.SetTarget(wax, Wedge1);
        Storyboard.SetTarget(way, Wedge1);

        Main.Resources.Add("animation", sb);

        sb.Begin();
    }

My XAML Image is like this: 我的XAML图像是这样的:

    <Image x:Name="Wedge1" Source="Images/Wedge.png" RenderTransformOrigin="-0.008,1.027" Height="682" Width="263" Canvas.Left="869.04" Canvas.Top="-158.251" >

        <Image.RenderTransform>
              <TransformGroup>
                    <ScaleTransform x:Name="wedge1scale" ScaleX="0.555" ScaleY="0.555"/>
                    <TranslateTransform X="88.102" Y="-4.381"/>
              </TransformGroup>
        </Image.RenderTransform>
    </Image>

Thanks in advance for any info :) 在此先感谢您的任何信息 :)

The problem is your PropertyPath . 问题是您的PropertyPath You would have to write RenderTransform.Children[0].ScaleX and RenderTransform.Children[0].ScaleY in order to animate the ScaleX and ScaleY properties of the first child of the TransformGroup in the Image's RenderTransform . 你将不得不写RenderTransform.Children[0].ScaleXRenderTransform.Children[0].ScaleY以动画ScaleXScaleY在图像的中的TransformGroup的第一个孩子的性能RenderTransform

var wax = new DoubleAnimation { Duration = TimeSpan.FromSeconds(1) };
var way = new DoubleAnimation { Duration = TimeSpan.FromSeconds(1) };

wax.To = ...
way.To = ...

Storyboard.SetTargetProperty(
    wax, new PropertyPath("RenderTransform.Children[0].ScaleX"));
Storyboard.SetTargetProperty(
    way, new PropertyPath("RenderTransform.Children[0].ScaleY"));

Storyboard.SetTarget(wax, Wedge1);
Storyboard.SetTarget(way, Wedge1);

var sb = new Storyboard();
sb.Children.Add(wax);
sb.Children.Add(way);
sb.Begin();

And it would be less code without the Storyboard: 如果没有情节提要,代码将会更少:

var wax = new DoubleAnimation { Duration = TimeSpan.FromSeconds(1) };
var way = new DoubleAnimation { Duration = TimeSpan.FromSeconds(1) };

wax.To = ...
way.To = ...

wedge1scale.BeginAnimation(ScaleTransform.ScaleXProperty, wax);
wedge1scale.BeginAnimation(ScaleTransform.ScaleYProperty, way);

And I guess in your case it isn't necessary to set the From property, as the animations start from the current property values by default. 而且我猜在您的情况下,没有必要设置From属性,因为默认情况下动画从当前属性值开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM