在WPF中,我看不出我的C#Storyboard和DoubleAnimation有什么问题

时间:2013-06-18 18:17:23

标签: c# wpf animation storyboard scaling

我尝试在C#而不是XAML中创建故事板来控制图像的缩放,因此我可以轻松更改ScaleTransform.ScaleX中的ScaleTransform.ScaleYDoubleAnimation值。

到目前为止,我相信我已经创建了动画并将其添加到新的故事板中,当我使用断点检查时,C#中的相应值会发生变化,但实际上并没有起作用。

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

我的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>

提前感谢任何信息:)

1 个答案:

答案 0 :(得分:1)

问题在于你PropertyPath。您必须编写RenderTransform.Children[0].ScaleXRenderTransform.Children[0].ScaleY,以便为图片ScaleX中TransformGroup的第一个子项的ScaleYRenderTransform属性设置动画。< / p>

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

没有故事板,代码就会减少:

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

我想在你的情况下没有必要设置From属性,因为默认情况下动画从当前属性值开始。