Silverlight不透明度动画无法以编程方式工作

时间:2012-03-05 15:40:14

标签: silverlight animation telerik opacity

我是Silverlight的新手,并尝试以编程方式为对象的不透明度设置动画的样本。

我想出了以下代码:

        MapShape myTestShape= RadMapInformationLayer.Items[0] as MapShape;
        SolidColorBrush brush = new SolidColorBrush();
        brush.Color = Colors.Purple;
        myTestShape.Fill = brush;

        //create a duration object
        Duration duration = new Duration(TimeSpan.FromSeconds(5));

        //create the storyboard
        Storyboard story = new Storyboard();

        //create double animation
        DoubleAnimation animation = new DoubleAnimation();

        //set the duration property
        animation.Duration = duration;

        //set the from and too values
        animation.From = 1.0;
        animation.To = 0.0;

        //add the duration to the storyboard
        story.Duration = duration;            

        //now set the target of the animation
        Storyboard.SetTarget(animation, myTestShape);

        //set the target property of this object
        Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty));

        //add the double animations to the story board
        story.Children.Add(animation);            

        if (!LayoutRoot.Resources.Contains("story1"))
            LayoutRoot.Resources.Add("story1", story);

        story.Begin();

对于属性路径,我也尝试过:

1.  new PropertyPath("(FrameworkElement.Opacity)")

2.  new PropertyPath("(FrameworkElement.Opacity)")

3.  new PropertyPath("(Control.Opacity)")

还有其他一些人,我对此没有好运。

谁能看到我哪里出错了?

谢谢, 雅克

1 个答案:

答案 0 :(得分:1)

之前我已经完成了对MapShape.FillProperty的数据绑定。

尝试:

        //create double animation
        ColorAnimation animation = new ColorAnimation();

        //set the duration property
        animation.Duration = duration;

        //set the from and too values
        animation.From = Colors.Purple;
        animation.To = Colors.Transparent;

        //add the duration to the storyboard
        story.Duration = duration;

        //now set the target of the animation
        Storyboard.SetTarget(animation, rect);

        //set the target property of this object
        Storyboard.SetTargetProperty(animation, new PropertyPath("(MapShape.Fill).Color"));

修改

至于为什么你的代码不起作用 - 通过MapShape.SetShapeFillStroke()来看,似乎Telerik不会将MapShape的Opacity绑定到其内部基本形状的Opacity,除非你提供填充。您是否在XAML中定义了填充?如果没有,请尝试提供一个。否则,可能在Shape的生命周期中(在ReadCompleted中或之后)过早定义代码?

<telerik:InformationLayer x:Name="StateLayer">
    <telerik:InformationLayer.Reader>
        ...
    </telerik:InformationLayer.Reader>
    <telerik:InformationLayer.ShapeFill>
        <telerik:MapShapeFill Fill="{StaticResource CommonBackgroundLightBrush}" Stroke="#5A636B" StrokeThickness="1" />
    </telerik:InformationLayer.ShapeFill>