【问题标题】:Silverlight opacity animation not working programmaticallySilverlight 不透明动画无法以编程方式工作
【发布时间】:2012-03-23 02:11:40
【问题描述】:

我是 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)")

还有其他一些人,我对此的运气为零。

谁能看出我哪里出错了?

谢谢, 雅克

【问题讨论】:

  • 我看不出您的代码有任何问题。当你执行它时会发生什么?有什么错误吗?例外?
  • 代码适用于矩形。也许这是为 MapShape 设置动画的问题。
  • 感谢回复 Foson 和 ColinE ColinE: 没有错误,只是没有做任何事情 Foson: 可能是 MapShape 对象,不知道它是否会改变你的想法,但它继承了来自 Control 对象?

标签: silverlight animation telerik opacity


【解决方案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 的不透明度绑定到其内部原始形状的不透明度,除非您提供填充。您是否在 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>

【讨论】:

  • 您好 Foson,我会尽快尝试一下。我的不透明代码不起作用的任何原因?
  • 您好 Foson,我已经尝试了您的代码,并且起作用的是地图形状变为透明,但随着时间的推移它绝对不是动画,它是瞬时的。 TimeSpan.FromSeconds(5)... 确实意味着 5 秒,对吗?
  • 更正:它根本不起作用,我实际上没有放入 PropertyPath 的 .Color 部分。当我添加它时,我得到一个异常:无法解析 TargetProperty (MapShape.Fill).Color on specified object。
猜你喜欢
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 2017-06-11
  • 1970-01-01
相关资源
最近更新 更多